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.

This project is archived and is in readonly mode.

:defaults not in path cause "No matching segment exists" error

#2478

This ticket is related to #1092 setting "arbitrary" default value on route foils named route generation (closed invalid).

The reviewer of #1092 setting "arbitrary" default value on route foils named route generation suggested I use :defaults in routes.rb. The pertinent advice is:

You can also define other defaults in a route by supplying a hash for the :defaults option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:

map.connect 'photos/:id', :controller => 'photos', :action => 'show', :defaults => { :format => 'jpg' }

With this route, an incoming URL of photos/12 would be dispatched to the show action within the Photos controller, and params[:format] will be set to jpg.

If you create a brand new Rails project and add that route and open the console you'll see that it doesn't work at all:


BillBurcham:Rails Bill$ rails foo
BillBurcham:Rails Bill$ cd foo
# Now edit routes.rb to include just the route described in the documentation above
BillBurcham:foo Bill$ ./script/console 
Loading development environment (Rails 2.3.2)
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/routing/builder.rb:107:in `assign_route_options':ArgumentError: format: No matching segment exists; cannot assign default

That's just the behavior I described in my December 23 comment to #1092 setting "arbitrary" default value on route foils named route generation.

So to recap:

The behavior described in the Agile book (see #1092 setting "arbitrary" default value on route foils named route generation) continues to partially work. It works for route recognition but is still broken in Rails 2.3.2 when it comes to path generation (the inverse of route recognition).

Furthermore, the alternative suggested by the reviewer of #1092 setting "arbitrary" default value on route foils named route generation in guides.rubyonrails.org doesn't work either. In fact its behavior is arguably worse than the behavior described in the Agile book because it breaks route recognition (not just path generation).

Reported by Bill Burcham · April 10th, 2009 @ 05:52 PM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.2, bug, routing to 2.3.2, bug, bugmash, routing

    August 6th, 2009 @ 01:41 PM

  2. Elise Huard
    Elise Huard

    reproduced on edge.
    when starting the server:
    actionpack/lib/action_controller/routing/builder.rb:109:in assign_route_options': format: No matching segment exists; cannot assign default (ArgumentError)<br/> I'm having a look.

    August 8th, 2009 @ 10:35 AM

  3. Elise Huard
    Elise Huard

    defaults produces an error when used with connect or named routes.

    defaults doesn't produce an error for resources or resource, where the segments argument of assign_route_options is an array with one element that respond to :key (and that key is 'format')
    but the default doesn't seem to be taken into account (or used) at all.

    I add tests that reproduces the error (and point towards a solution, maybe), but am not about to start messing about with the structures of segments etc quite yet.

    August 8th, 2009 @ 11:56 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • Assigned user set to josh

    August 10th, 2009 @ 02:08 AM

  5. josh
    josh
    • State changed from new to open
    • Milestone changed from 2.x to 2.3.4

    August 10th, 2009 @ 02:12 AM

  6. josh
    josh
    • State changed from open to incomplete

    August 19th, 2009 @ 03:59 PM

  7. josh
    josh
    • State changed from incomplete to stale

    August 31st, 2009 @ 06:58 PM

  8. Andrew White
    Andrew White
    • Milestone cleared.
    • State changed from stale to resolved
    • Assigned user changed from josh to Andrew White

    This appears to be working now:

      # routes.rb
      resources :users, :defaults => { :locale => 'en' }
    
      andyw$ rails c
      >> app.users_path
      => "/users"
    

    Please update this ticket if it isn't working for you.

    June 25th, 2010 @ 03:46 PM

  9. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:56 PM

  10. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:53 AM

  11. Jeremy Kemper
  12. Jeff Kreeftmeijer