now im having errors trying to get any of the actions inside my
question controller.
question/new
question/edit
etc… all come up with errors trying to send it to the action ‘show’
but why is the :id => /\d+/ needed? and what does that do ?
does it check to see if its a decimal as a parameter or something ?
yes.
when you’ve got this named route :
map.question ‘question/:id’, :controller => ‘question’, :action =>
‘show’
with ‘question/new’, Rails will try to match this path with your route,
and it will be successful with params[:id] = ‘new’.
If you require that the dynamic segment (:id) in the route must be in
decimal format ( :id => /\d+/), then ‘question/new’ will not be
recognized by the question named route, and Rails will try
with next route and so on. ‘question/new’ will be later recognized
by ‘:controller/:action/:id’ route.
mm cos i thought rails would put preference to actions if one exists
so i thought it would work like this.
if we have ‘question/123’
look for an action by the name of 123 , if not then see what other
routes we have, in this case we have
map.question ‘question/:id’, :controller => ‘question’, :action =>
‘show’
Not exactly. The way it works, as far as I know, is that Rails
tries to make your path recognize from routes, from the top to
the bottom, and stops when there is a match. If any routes don’t
recognize your path, there’s an error. If a path can be recognized
by many routes, the one with the highest priority wins (because
Rails starts at the top and stop at the first match). So the order
of your routes is important.
Also :
in your class controller, it’s possible to use a method_missing
mechanism
it’s also possible that the method doesn’t exist but
as the template exists, Rails will directly render it (I never use
this mechanism but it should work)
and in your routes.rb, you can if you want, suppress the generic
route :
map.connect ‘:controller/:action/:id’
because you find it permissive and you’d rather defining all the
routes ‘statically’.