Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Roger Helliwell
Roger Helliwell
6,697 Points

Why does "redirect_to @post" not work for me in this example.

The post gets created (when I refresh the page I see the new comment), but the redirect_to @post doesn;'t seem to do anything. In my browser, the page stays the same with the "Add Comment" button grey-ed out.

In the console output, there is a warning that the 'create' template was not found. (But we are simply POSTing to 'create', and should be redirecting back to the post.

Any ideas?

Started POST "/posts/1/comments" for ::1 at 2021-04-08 14:03:04 -0400
Processing by CommentsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "comment"=>{"name"=>"", "content"=>""}, "commit"=>"Create Comment", "post_id"=>"1"}
  Post Load (0.1ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/comments_controller.rb:3:in `create'
  TRANSACTION (0.0ms)  begin transaction
  ↳ app/controllers/comments_controller.rb:4:in `create'
  Comment Create (0.4ms)  INSERT INTO "comments" ("content", "name", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["content", ""], ["name", ""], ["post_id", 1], ["created_at", "2021-04-08 18:03:04.997190"], ["updated_at", "2021-04-08 18:03:04.997190"]]
  ↳ app/controllers/comments_controller.rb:4:in `create'
  TRANSACTION (1.1ms)  commit transaction
  ↳ app/controllers/comments_controller.rb:4:in `create'
No template found for CommentsController#create, rendering head :no_content
Completed 204 No Content in 8ms (ActiveRecord: 1.6ms | Allocations: 3310)

1 Answer

You want to redirect to a show path and give @post as the specific post as an argument some_path(@instance)