views:

40

answers:

3

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?

A: 

In the controller, you should just give an error if params[:id] is nil.

mathepic
A: 

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]
Salil
A: 

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.

dsueiro