Reputation: 151264
In routes.rb
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
but then
http://localhost:3000/foobars/alt
will work too. The params are:
{"action"=>"alt", "controller"=>"foobars"}
so looks like :id
is optional here. How can you tell it is optional vs not optional? Can you make it so that it is not optional in routes.rb?
Upvotes: 2
Views: 632
Reputation: 173
You can use :requirements to impose some conditions on :id for the route to match, ie: map.connect ':controller/:action/:id', :requirements => { :id => /\d{4}/ } . That way, if :id doesn't match the requirement, the route won't match either.
Upvotes: 1
Reputation: 47542
http://localhost:3000/foobars/alt
To achieve this path you have to mention this route in routes.rb.
something like following
map.resources :foobars, :collection=> {:alt=>:get }
otherwise it will treat alt as params[:id] and if you create controller using scaffolding it will go to the show
action of your foobars
controller.
So for above mention url it becomes optional and treat it as an action insetad of params[:id]
Upvotes: 0
Reputation: 13042
In the controller, you should just give an error if params[:id]
is nil
.
Upvotes: 0