This project is archived and is in readonly mode.
[PATCH] Defaults support for the new Router DSL
Reported by VirtualFunction | March 25th, 2010 @ 06:19 AM | in 3.0.2
There is not way to be able to set default values for the params hash in Rails 3.0
In Rails 2.x I had something like this
map.with_options :controller => 'pages', :action => 'show' do |info|
info.home_page '/', :id => 'about'
[ 'services', 'support', 'podcasts', 'partners', 'disclaimer', 'privacy', 'design', 'public' ].each do |page|
info.__send__ "#{page}_page", "/#{page}", :id => page
end
end
As you can see, I'm supplying a default value for the :id in the params hash. (Yes, I know it's pretty ugly given it's use of send, but it work nicely)
In Rails 3.0 there is no way I can do this.
I can do something like:
[ 'services', 'support', 'podcasts', 'partners', 'disclaimer', 'privacy', 'design', 'public' ].each do |page|
match page => 'pages#show', :as => "#{page}_page"
end
Which sets up named routes that I can use, but there is no way the :id param can be passed into the controller
or I can do something like:
match ":id" => 'pages#show', :constraints => { :id => /services|support|podcasts|partners|disclaimer|privacy|design|public/ }
This allows me to pass :id properly via the constraints option, but doesn't allow me to create friendly named routes.
It seems I can have one or the other, but not both.
Would it not be possible to do something like:
[ 'services', 'support', 'podcasts', 'partners', 'disclaimer', 'privacy', 'design', 'public' ].each do |page|
match page => 'pages#show', :as => "#{page}_page", :defaults => { :id => page }
end
I see internally there is still support for defaults as the :to => 'controller#action' notation is translated internally by ActionDispatch::Routing::Mapper::Mapping#defaults
Comments and changes to this ticket
-
Andrew White March 25th, 2010 @ 06:09 PM
- Assigned user set to josh
Yep, I can confirm this. It also means you can't do routes with optional params, e.g:
# Old style map.page 'pages/:id', :controller => 'pages', :action => 'show', :id => 'default' # GET /pages -> { :controller => 'pages', :action => 'show', :id => 'default' } # New style match 'pages/:id' => 'pages#show', :id => 'default' # GET /pages -> Routing Error
I thought it may be intentional but even if you tried to add a match for 'pages' you'd have to route it to a different action that set a default :id. I can come up with a patch but some feedback would be nice on whether only defaults in an explicit sub hash (e.g :defaults => { }) or any extra keys in the options hash as well. Regexp requirements can still be set in the options hash so I'm guessing that defaults should be the same.
-
Andrew White March 26th, 2010 @ 07:36 AM
Oops - optional params are now specified using parentheses, so I take that back about not being able to specify them.
-
Andrew White March 26th, 2010 @ 12:49 PM
Okay the attached patch adds defaults support to the new routing DSL allowing the following:
root :to => 'pages#show', :id => 'home' %w(about contact services).each do |page| match page => 'pages#show', :as => "#{page}_page", :id => page end
It supports specifying defaults in the options hash, in a defaults hash as part of the options hash and also as a scope.
-
Rizwan Reza March 26th, 2010 @ 01:56 PM
- Milestone cleared.
- Tag set to 3.0, bug, router
- State changed from new to verified
- Title changed from Unable to specify default values for params hash to [PATCH] Defaults support for the new Router DSL
+1
Patch applies cleanly and all tests pass. Fixes the issue. Nice work, Andrew.
-
Repository March 27th, 2010 @ 07:21 AM
- State changed from verified to resolved
(from [3d746fcdb584767c476408f395320e934fd5383e]) Add parameter defaults support to new routing DSL [#4265 state:resolved]
Signed-off-by: wycats wycats@gmail.com
http://github.com/rails/rails/commit/3d746fcdb584767c476408f395320e... -
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Medium
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 4131 Routing :defaults option is not passed through to params I have reran the tests against master and everything work...
- 4265 [PATCH] Defaults support for the new Router DSL (from [3d746fcdb584767c476408f395320e934fd5383e]) Add par...