开发者

Rails 3 routing

Ok, I'm relatively new to rails and am working through the book Rails for PHP programmers. To make things interesting I'm using Rails 3 and haml to work through the book, and the book is written using Rails 2.X and erb so some of the examples are outdated.

To the question: The route example in the book is

map.presentation 'meetings/:meeting_id/presentations/:action/:id',
    :controller => "presentations",
    :action => "show",
    :meeting_id => /\d+/

So that was a pre-Rails 3 route. My adaptation of the above into a Rails 3 route is:

match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations#show', :constraints => {:id => /\d/}

My adaptation works for the destroy action, but not for the edit action... and due to my inexperience I don't know what I'm doing wrong. From the article here (http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/) it looks like I'm doing it right, but something isn't right.

The link_to helpers that produce the urls are as follows

= link_to 'edit',   :controller => 'presentations',
                    :action     => 'edit',
                     :meeting_id => presentation.meeting.id,
                     :id            => presentation.id
# Incorrectly Produces: http://localhost:3000/presentations/edit?meeting_id=2&id=1

= link_to 'destroy', {  :controller => 'presentations',
                        :action     => 'destroy',
                        :meeting_id => presentation.meeting.id,
                        :id         => presentation.id },
                    :confirm => 'Are you sure?',
                    :method => :delete
# Correctly Produces: http://localhost:3000/meetings/2/presentations/destroy/1

Your help would go a long way to clearing up my routing haziness. Thanks so much!

EDIT

Here's the non-working routes.rb file in its entirety:

UserGroup::Application.routes.draw do
  get "presentations/new"
  get "presentations/edit"
  get "sessions/new"
  get "users/index"
  get "users/show"
  get "users/new"
  get "users/edit"
  get "meetings/index"

  match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
  match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
  match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'

  match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}

  #default route
  match ':controller(/:action(/:id(.:format)))'
end

EDIT 2

Thanks to codykrieger I took a look at the rest of the routes file (I know, duh right?). Apparently, all those get "..." routes are added when开发者_如何学Go using the rails generator and help to make some default connections in the app. I commented out the get "presentations/edit" line and, wonder of wonders, my routing adaptation actually does work.

This works:

UserGroup::Application.routes.draw do
  get "presentations/new"
  #get "presentations/edit"
  get "sessions/new"
  get "users/index"
  get "users/show"
  get "users/new"
  get "users/edit"
  get "meetings/index"

  match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
  match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
  match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'

  match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}

  #default route
  match ':controller(/:action(/:id(.:format)))'
end

I played around with the order of the routes in the file and also found that if I put my route above all the auto-generated routes, without commenting out the get "presentations/edit" line, my route still has the intended effect.

This also works:

UserGroup::Application.routes.draw do

  match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}

  get "presentations/new"
  get "presentations/edit"
  get "sessions/new"
  get "users/index"
  get "users/show"
  get "users/new"
  get "users/edit"
  get "meetings/index"

  match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
  match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
  match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'


  #default route
  match ':controller(/:action(/:id(.:format)))'
end

I'm thinking that the latter is the better way to go and I should make my custom routing declarations above the standard generated ones, but I'm not sure. If someone out there wants to comment on which practice is better, or if it's better to entirely remove the generated ones if I want to specify my own, I'd love to hear it.

Thanks all :)


Routing is done on a first match basis (top to bottom) in the routes.rb file. So you can keep all routes as you have done above if you want, and Rails will use the first one that matches. However, its messy, potentially buggy and totally unnecessary. So the "match..." lines in your code should be enough, and you can remove the "get..." lines.

Furthermore, your "match...presentations...." line has a bug, it should be

...{:id => /\d+/}

else it will validate only 1-digit length ids.

Finally, as far as your overall routes.rb is concerned, you do not need to do

link_to 'edit', :controller => 'presentations', :action => 'edit', :meeting_id => presentation.meeting.id, :id => presentation.id

Instead, you should use edit_meeting_path, new_meeting_path etc. For instance

<%= link_to "New meeting", new_meeting_path %>
<%= link_to "Edit meeting", edit_meeting_path(@meeting) %>

Finally, you should read Rails Routing from the Outside In for best practices on how to design nested routes etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜