嵌套路由的规范

时间:2016-09-28 10:53:28

标签: ruby-on-rails ruby-on-rails-4 routing routes

我的路线是:

resources :posts
  resources :images
end

我想为ImagesController编写规范:

it "renders the index template" do
  get :index, {:id => @post.id}
  expect(response).to render_template("index")
end

错误是

No route matches {:action=>"index", :controller=>"images", :id=>"19"}

我该如何解决这个问题?

编辑:

已修复post_id,但似乎仍未获取参数:

新错误

Failure/Error: get :index, {post_id: @post.id}
     NoMethodError: undefined method `+' for nil:NilClass

索引视图:

  def index
    @post = Post.find(params[:post_id])
    @calculations = @post.something + 1
  end

1 个答案:

答案 0 :(得分:0)

您应该传递post_id参数:

get :index, post_id: @post.id

修改

在您使用正确的信息更新问题后,我们可以看到,您的代码存在一些问题:

@post = Post.find(params[:post_id]) # should be Post.find(params[:id])
@calculations = @post.something + 1 # thus this is `nil` + 1 - error
相关问题