In routes.rb
i have:
resources :themes do
resources :messages
end
In messages_controller_test.rb
i have:
setup do
@theme = themes(:one)
@message = messages(:one)
end
test "should create message" do
assert_difference('Message.count') do
post :create, message: { title: "Title", body: "Some body", theme_id: @theme.id }
end
assert_redirected_to theme_path(@theme)
end
And i'm getting the error: Couldn't find Theme without an ID
What's going wrong?
With nested resources, the create route looks like this:
/themes/:theme_id/messages
so you need to pass these params:
test "should create message" do
assert_difference('Message.count') do
post :create, {theme_id: @theme.id, message: { title: "Title", body: "Some body", theme_id: @theme.id }}
end
assert_redirected_to theme_path(@theme)
end
And actually in your controller action you can handle that :theme_id param in order not to pass it anymore in params[:message]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.