Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

[PATCH] Defaults support for the new Router DSL

#4265

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

Reported by VirtualFunction · March 25th, 2010 @ 06:19 AM

State: resolved
Milestone: 3.0.2
Assigned to: josh josh
Importance: Medium

Activity

  1. Andrew White
    Andrew White
    • 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.

    March 25th, 2010 @ 06:09 PM

  2. Andrew White
    Andrew White

    Oops - optional params are now specified using parentheses, so I take that back about not being able to specify them.

    March 26th, 2010 @ 07:36 AM

  3. Andrew White
    Andrew White

    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.

    March 26th, 2010 @ 12:49 PM

  4. Rizwan Reza
    Rizwan Reza
    • 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.

    March 26th, 2010 @ 01:56 PM

  5. Repository
  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Medium

    October 15th, 2010 @ 11:01 PM