Jeff Storey
Jeff Storey

Reputation: 57192

rails redirect_to post

I saw an example in the book The Rails 3 Way that says

redirect_to post

Does this have some special meaning because of the post, or if it is just a poor choice for an example and the post is just a domain object and it's redirecting to the url for that object.

Upvotes: 4

Views: 1499

Answers (3)

James Martin
James Martin

Reputation: 56

you need a Post resource and the corresponding routes in config/routes.rb

just do "rails g resource post title:string content:text" for example to generate one. the route in "redirect_to post" is handled dynamically. the instance of Post "post" is passed in as the argument to the redirect_to method, causing the page to redirect to the posts#show action and passing the :id of that instance of Post. it's "rails magic"

Upvotes: 0

Bryce
Bryce

Reputation: 2872

I would need to see the full example for a complete answer, but my guess is that the author just picked "Post" as the name of one of the models and didn't realize it might cause confusion to the reader with the POST action.

As part of the HTTP Protocol (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html), you actually can't redirect to a POST action. Or, more explicitly, whatever destination you are calling with a redirect needs to return with the GET method.

Hope that helps!

Upvotes: 8

Иван Бишевац
Иван Бишевац

Reputation: 14641

Look at Rails Routing Guide then it should be clear why that is ok.

Also if you want to see routes for your application run:

cd path/to/your/app
rake routes

This will list routes available in your app.

Upvotes: 1

Related Questions