I have a nested route:
resources :stories do
resources :comments
end
this is my create method in controller:
def create
@story = Story.find(params[:story_id])
@comment = @story.comments.build(params[:comment])
@comments = @story.comments.all
respond_to do |format|
if @comment.save
format.html { redirect_to @story, notice: t('controllers.comments.create.flash.success') }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render template: "stories/show" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
And here is it's test:
setup do
@comment = comments(:one)
@story = stories(:one)
end
....
test "should create comment" do
assert_difference('Comment.count') do
post :create, :story_id => @story.id, comment: { content: @comment.content, name: @comment.name, email: @comment.email }
end
assert_redirected_to @story_path
end
that ends up with this error:
1) Failure:
test_should_create_comment(CommentsControllerTest) [/home/arach/workspace/Web/ruby/nerdnews/test/functional/comments_controller_test.rb:25]:
Expected response to be a redirect to <http://test.host/stories/980190962/comments> but was a redirect to <http://test.host/stories/980190962>
I don't know why the test expect to redirect to stories/:id/comments
. I tried other things like story_comment_path
but it didn't help either. story_path
without @
also ends up with another error:
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"stories"}
same error happens for story_path, :story_id => @story.id
. Any idea why this happens?
I think it should be story_path(@story.id). See here .
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.