0

ネストされたルートがあります:

resources :stories do
  resources :comments
end

これはコントローラーの私の作成方法です:

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

そして、これがテストです:

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

それはこのエラーで終わります:

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>

テストが にリダイレクトすることを期待する理由がわかりませんstories/:id/comments。などの他のことを試しstory_comment_pathましたが、どちらも役に立ちませんでした。story_pathなし@でも別のエラーが発生します:

ActionController::RoutingError: No route matches {:action=>"show", :controller=>"stories"}

に対しても同じエラーが発生しstory_path, :story_id => @story.idます。なぜこれが起こるのか分かりますか?

4

1 に答える 1

1

story_path(@story.id)にすべきだと思います。ここを参照してください。

于 2012-07-29T19:43:12.890 に答える