Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRoger Helliwell
6,697 PointsWhy 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
Michael Knott
2,041 PointsYou want to redirect to a show path and give @post as the specific post as an argument some_path(@instance)