Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 routing
    primarykey
    data
    text
    <p>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.</p> <p>To the question: The route example in the book is</p> <pre><code>map.presentation 'meetings/:meeting_id/presentations/:action/:id', :controller =&gt; "presentations", :action =&gt; "show", :meeting_id =&gt; /\d+/ </code></pre> <p>So that was a pre-Rails 3 route. My adaptation of the above into a Rails 3 route is:</p> <pre><code>match 'meetings/:meeting_id/presentations/:action/:id', :to =&gt; 'presentations#show', :constraints =&gt; {:id =&gt; /\d/} </code></pre> <p>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.</p> <p>The <code>link_to</code> helpers that produce the urls are as follows</p> <pre><code>= link_to 'edit', :controller =&gt; 'presentations', :action =&gt; 'edit', :meeting_id =&gt; presentation.meeting.id, :id =&gt; presentation.id # Incorrectly Produces: http://localhost:3000/presentations/edit?meeting_id=2&amp;id=1 = link_to 'destroy', { :controller =&gt; 'presentations', :action =&gt; 'destroy', :meeting_id =&gt; presentation.meeting.id, :id =&gt; presentation.id }, :confirm =&gt; 'Are you sure?', :method =&gt; :delete # Correctly Produces: http://localhost:3000/meetings/2/presentations/destroy/1 </code></pre> <p>Your help would go a long way to clearing up my routing haziness. Thanks so much!</p> <p><strong>EDIT</strong></p> <p>Here's the non-working routes.rb file in its entirety:</p> <pre><code>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 =&gt; 'meetings#show', :as =&gt; 'meeting' match '/meetings/new', :to =&gt; 'meetings#new', :as =&gt; 'new_meeting' match '/meetings/edit/:id', :to =&gt; 'meetings#edit', :as =&gt; 'edit_meeting' match 'meetings/:meeting_id/presentations/:action/:id', :to =&gt; 'presentations', :constraints =&gt; {:id =&gt; /\d/} #default route match ':controller(/:action(/:id(.:format)))' end </code></pre> <p><strong>EDIT 2</strong></p> <p>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 using the rails generator and help to make some default connections in the app. I commented out the <code>get "presentations/edit"</code> line and, wonder of wonders, my routing adaptation actually does work.</p> <p>This works:</p> <pre><code>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 =&gt; 'meetings#show', :as =&gt; 'meeting' match '/meetings/new', :to =&gt; 'meetings#new', :as =&gt; 'new_meeting' match '/meetings/edit/:id', :to =&gt; 'meetings#edit', :as =&gt; 'edit_meeting' match 'meetings/:meeting_id/presentations/:action/:id', :to =&gt; 'presentations', :constraints =&gt; {:id =&gt; /\d/} #default route match ':controller(/:action(/:id(.:format)))' end </code></pre> <p>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 <code>get "presentations/edit"</code> line, my route still has the intended effect.</p> <p>This also works:</p> <pre><code>UserGroup::Application.routes.draw do match 'meetings/:meeting_id/presentations/:action/:id', :to =&gt; 'presentations', :constraints =&gt; {:id =&gt; /\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 =&gt; 'meetings#show', :as =&gt; 'meeting' match '/meetings/new', :to =&gt; 'meetings#new', :as =&gt; 'new_meeting' match '/meetings/edit/:id', :to =&gt; 'meetings#edit', :as =&gt; 'edit_meeting' #default route match ':controller(/:action(/:id(.:format)))' end </code></pre> <p>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.</p> <p>Thanks all :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload