views:

49

answers:

1

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?

A: 

Why not using a resource style? It's made for that.

shingara
Yes you are absolutely correct that I can use resource style. But since I am in the middle of learning rails routing, I think it will do no harm if I go a little bit further into the way it works. So though this question may seen a fool to you, it is one from a learner so please be tolerant and explain if you can.
Steve
So I suppose it's a problem with _method and HTTP method use to send answer.
shingara