0

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?

1 Answer 1

1

I think it should be story_path(@story.id). See here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :)) How did I miss that :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.