Matt Jones
2008-Nov-07 21:59 UTC
Better documentation for passing extra parameters to routes
I just spend a while trying to figure this out - the documentation in
routing.rb is out of date.
A route like:
map.connect '':controller/:action/:id'', :action =>
''show'', :defaults =>
{ :page => ''Dashboard'' }
will blow up with an error. (`assign_route_options'': page: No matching
segment exists; cannot assign default (ArgumentError)).
Turns out, after digging around, that the correct way to do this is now:
map.connect '':controller/:action/:id'', :action =>
''show'', :page =>
''Dashboard''
--
On a related note, it isn''t documented *anywhere* that you can pass
extra parameters like this to
resource routes, if you use requirements:
map.resources :things, :requirements => { :extra_param =>
''whatever'' }
Which will call all the REST actions with :extra_param =>
''whatever''.
It''s really handy for cases where you might want
to disambiguate multiple paths to the same resource, ie:
map.resource :library do |p|
p.resources :comments, :requirements => {:type =>
''library''}
end
map.resource :archive do |p|
p.resources :comments, :requirements => {:type =>
''archive''}
end
Both /library/comments and /archive/comments will map to
CommentsController, so the :type will tell the two apart.
(Note that plural resources get a (resource)_id value, but singular
resources do not.)
--
The syntax above for named routes is a little odd; it doesn''t seem
that many people would guess that :requirements was
what they''re looking for. Thoughts?
--Matt Jones
al2o3cr@gmail.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---
> I just spend a while trying to figure this out - the documentation in > routing.rb is out of date.Please do add a patch to docrails with all the information you''ve learned. Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---