2016-09-28 71 views
0

我的路线是:规范嵌套路线

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 
+0

'得到:指数:POST_ID => @post .id' –

+0

@AndreyDeineko'故障/错误:得到:指数,{:POST_ID => @ post.id} NoMethodError: 未定义的方法'+”为零:NilClass' 和我有@post definied当然 –

+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 
+0

'params [:post_id]'很好,因为它更加嵌套。感谢您对第一个错误的帮助,我自己调试了其余的部分! –

+0

@ J.Doe从你的问题中不知道'resources:posts'也是嵌套的。但是你发现它很好 –