Newbie Routing Question /site/1/edit vs. /site/edit/1

I have a question about how URLs are created.

This happens with the edit action on the controller.

Instead of creating the URL as http://site.com/edit/1 it sticks the ID
in
the middle:
http://site.com/1/edit

Two questions:

  1. What exactly is the reason behind this default?
  2. How do I override it?

I’ve tried searching but I’m not sure what terminology that I should
search
for: “default route/routing”, “route edit”, etc…didn’t turn up
anything
useful.

ThanX in advance.

are you using restful routes? In any case, that’s quite strange… The
default should be

:controller/:action/:id

You’re also missing the controller name for some reason.

is this a new rails application? Have you messed around with the
routes file? Do all your urls for different controllers show up this
same way? Are you using nested resources?

Baz wrote:

Instead of creating the URL as http://site.com/edit/1 it sticks the ID
in
the middle:
http://site.com/1/edit

This should be more like:

http://site.com/controller/1/edit

  1. What exactly is the reason behind this default?

These are RESTful routes. Google for “rails REST routes” or “rails 2
scaffolding” and you should get some useful info. Basically, it’s a way
of treating your database objects as resources and performing actions on
them. Many actions involve just the HTTP method (GET, POST, PUT,
DELETE) (the verbs of the request) and either the controller name or the
controller plus an object’s id field (the nouns of the request). The
appended actions (like “edit” above) extend this “language”. This means
that the resource is:

http://site.com/controller/1

and the action to be performed is encoded in the HTTP method (possibly
augmented by an appended action to the resource). To see how this is
put together, run the following command from your application’s top
level directory:

rake routes

  1. How do I override it?

These resources are used when you have lines like this in your
config/routes.rb file:

map.resources :controller

If you comment this out, you will see that there is a line later which
reads:

map.connect ‘:controller/:action/:id’

which will then provide you with the form of URLs you are looking for.
Running the rake command again will show the difference.

Right on the money Mark. ThanX.

Yes, I was missing the site.com/controller/edit/1

ThanX

I don’t take it there is a way to scaffold without these?

eg. make it generate:
link_to ‘Show’, :action => ‘show’, :id => controller
?

On Wed, Mar 12, 2008 at 3:13 AM, Mark B.
[email protected]