This project is archived and is in readonly mode.

#2478 ✓resolved
Bill Burcham

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

Reported by Bill Burcham | April 10th, 2009 @ 05:52 PM | in 3.0.2

This ticket is related to #1092 (closed invalid).

The reviewer of #1092 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.

So to recap:

The behavior described in the Agile book (see #1092) 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 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).

Comments and changes to this ticket

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>

Attachments

Referenced by

Pages