I am having a hard time understanding routes in rails 3. I created two scaffolds: Users and magazines. The users are able to login, but I am unable to link to the magazine page. I know it has to do with creating a route. If I navigate via the URL to localhost:3000/magazines, I can see the multiple magazines I created and each user associated with each magazine. I just can't seem to connect the dots. I want to create a link from the user page to the magazine page. I know this is basic, but all the routes documentation just are not making sense to me. Thanks so much for your time.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Backbone.js PushState routes .htaccess only workin
- Eager-loading association count with Arel (Rails 3
- Laravel - Implicit route model binding with soft d
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
This is a really nice summary of the routes: Rails Routing from the Outside In.
You also might want to check these RailsCasts:
http://railscasts.com/episodes/203-routing-in-rails-3 ,
http://railscasts.com/episodes/231-routing-walkthrough ,
http://railscasts.com/episodes/232-routing-walkthrough-part-2
and these pages:
http://edgeguides.rubyonrails.org/routing.html
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
http://markconnell.co.uk/posts/2010/02/rails-3-routing-examples
By far, the best explanation of routes I've found is in The Rails 3 Way by Obie Fernandez (founder of Hash Rocket).
Resource pointed out in previous answers is awesome and that is where I got started. I still refer that in case I am stuck somewhere. One thing I find missing in the recourse is that it doesn't include the explanation of reading the routes table i.e. output of command
rake routes
and it takes time to fit the pieces together. Although if you read through the whole guide patiently, you can fit the pieces together.On my system 'rake routes' gives the following output (excerpt relevant to
resources :messages
)All the columns in this table give very important information:
messages_path
andmessages_url
in your views and controllers as a helper method. Looking at the table you can tellmessages_path
will generate a path of form "/messages(.:format)". Similarly, other route names generated are "new_message", "edit_message" and "message". You can also control the naming of routes.messages_path
with http "GET" it would match first route and if you use it with "POST" it will match second route. This is very important to note, same url with different http verbs can map to different routes.(.+)
and captures the match in parameter "id", so that you can do something likeparams[:id]
and get the captured string. Braces()
represent that this parameter is optional. You can also pass these parameters in helpers to generate the corresponding route. For example if you usedmessage_path(:id => 123)
is will generate the output "/messages/123".So if "localhost:3000/magazines" is the page you want, you should check the routes table with url pattern as "/magazines(.:format)" and disect it yourself to find out what you need. I would recommend you read the whole guide from top to bottom if you are just starting rails.
(This might be just an overkill to write all this here, but I faced many problems due to this info not being available in a consolidated manner. Always wanted to write it out and finally did. I wish it was available on http://edgeguides.rubyonrails.org/routing.html in a separate section.)
A few things, in addition to what others have already said:
magazines_path
is the most likely name of the link to the index page.so that should do the trick. But if you want to see routes, I'd recommend you just run
rake routes
, which will list whatever Rails is considering a valid route name. If you want to see how they're used, check out the view pages for your scaffold.app/views/magazines/show.html.erb
, for example, might have something like this at the bottom:The edit link goes to the
edit
page (/magazines/[ID]/edit
) for the magazine stored in@magazine
and the back link goes to the index page (/magazines/
). The show page for an individual magazine would bemagazine_path(@magazine)
and the new path would benew_magazine_path(@magazine)
.You should definitely check out the resources others have posted -- Rails routing is flexible but very "magical" -- but in any case, that should help give you some context.
Also, this should be automatically generated, but I think most people are assuming your
config/routes.rb
contains something like the following:This is what tells rails to build out the basic routes for index, new, edit, show, create, update, destroy for a particular resource.
What about:
You should be aware of all routes created by the mere scaffold. It's quite easy and explained in Rails guides.
Here are the details: http://guides.rubyonrails.org/routing.html#paths-and-urls