Rails routing problem
- by Steve
I am new to Rails routing and I currently have a problem and hope someone can explain it to me. I am using Rails 2.3.5
Firstly, let me describe my working-fine code:
I have a text example, which has a controller (cars_controller) with an update action (along with some other actions). The update action needs the :id parameter. The edit.html.erb has a form:
<% form_for :car, :url = {:controller = 'cars', :action = 'update' } %
... # rest of the form content.
In the configuration/routes.rb, I have a self-defined routing rule for update:
map.connect 'car/update/:id', :controller = 'cars', :action = 'update'
This works fine.
Secondly, I change the code. All I change is the self-defined routing rule to
map.connect 'car/:action/:id, :controller = 'cars'
To me, this rule covers the self-written routing rule. Of course, this rule is also used by other actions such as edit. But the edit.html.erb doesn't work. It complains that update action misses the :id parameter. I have to change the form_for helper to:
<% form_for :car, :url = {:controller = 'cars', :action = 'update', :id = @car }% ... # @car is the instance passed to edit view.
I know that if missing the :id parameter, update action will complain. What I don't understand is why my first code works (with my self-defined routing rule) but my second code fails. It seems to me that I didn't provide :id parameter in my self-defined routing rule. Anyone has an idea?