ruby on rails routes

2020-06-27 06:48发布

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.

6条回答
Juvenile、少年°
2楼-- · 2020-06-27 07:20

This is a really nice summary of the routes: Rails Routing from the Outside In.

查看更多
趁早两清
4楼-- · 2020-06-27 07:25

By far, the best explanation of routes I've found is in The Rails 3 Way by Obie Fernandez (founder of Hash Rocket).

查看更多
唯我独甜
5楼-- · 2020-06-27 07:29

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)

    messages GET    /messages(.:format)            {:action=>"index", :controller=>"messages"}
             POST   /messages(.:format)            {:action=>"create", :controller=>"messages"}
 new_message GET    /messages/new(.:format)        {:action=>"new", :controller=>"messages"}
edit_message GET    /messages/:id/edit(.:format)   {:action=>"edit", :controller=>"messages"}
     message GET    /messages/:id(.:format)        {:action=>"show", :controller=>"messages"}
             PUT    /messages/:id(.:format)        {:action=>"update", :controller=>"messages"}
             DELETE /messages/:id(.:format)        {:action=>"destroy", :controller=>"messages"}

All the columns in this table give very important information:

  • Route Name(1st Column): This gives the name of the route, to which you can append "_url" or "_path" to derive the helper name for the route. For example, first one is the "messages", so you can use messages_path and messages_url in your views and controllers as a helper method. Looking at the table you can tell messages_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.
  • HTTP Verb(2nd Column): This gives the information about the http verb which this route will respond to. If it is not present, then it means this route will respond to all http verbs. Generally browsers only support, "GET" and "POST" verbs. Rails simulate "PUT" and "DELETE" by passing a parameter "_method" with verb name as value to simulate "PUT" and "DELETE". Links by default result in a "GET" verb and form submissions in "POST". In conjunction with the first column, if you use 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.
  • URL Pattern(3rd Column): Its like a limited featured regular expression with syntax of its own. ":id" behaves like (.+) and captures the match in parameter "id", so that you can do something like params[: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 used message_path(:id => 123) is will generate the output "/messages/123".
  • Where this routes(4th Column): This column generally tells the controller and the corresponding action which will handle requests matching this route. There can be additional information here like constraints if you defined any.

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.)

查看更多
Rolldiameter
6楼-- · 2020-06-27 07:34

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.

<%= link_to "Magazines", magazines_path %>

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:

<%= link_to 'Edit', edit_magazine_path(@magazine) %> |
<%= link_to 'Back', magazines_path %>

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 be magazine_path(@magazine) and the new path would be new_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:

My::Application.routes.draw do
  resources :magazines
  resources :users
  # or the above combined as resources :magazines, :users
end

This is what tells rails to build out the basic routes for index, new, edit, show, create, update, destroy for a particular resource.

查看更多
欢心
7楼-- · 2020-06-27 07:35

What about:

<%= link_to "magazines", magazines_path %>

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

查看更多
登录 后发表回答