2016-09-28 70 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'也是嵌套的。但是你發現它很好 –