Search code examples
ruby-on-railsruby-on-rails-4ruby-on-rails-2

Differences betwen 2.3 and 4.2.6 routing


In 2.3, there's

  map.connect ':controller/:action/:id'

I tried to access for example "/resource/edit", it has no problem to find controller resource and action edit.

In 4.2, I use

  get ':controller/:action/:id'

The "/resource/edit" does not work. I guess it is because there's no :id?

If I change to

get ':controller/:action(/:id)'

which makes :id optional, then everything works.

So my questions are:

1) is my guess correct? Which is: for 2.3 you don't need to define optional, and it will be optional, but for 4.2 you must define optional to make it optional.

2) For 2.3, once

  map.connect ':controller/:action/:id'

is defined, is it defined for all GET/POST/PATCH/DELETE etc? For 4.2, I must define the following?

get ':controller/:action(/:id)'
post ':controller/:action(/:id)'

etc one by one?

  1. Is there any other big differences you can think of?

Thanks.


Solution

  • (1) Yes, this is correct.

    (2) Use resourceful routing as an alternative to defining, GET, POST, PATCH, DELETE on the same route:

    resources :resource_name
    

    this will define new (GET), create (POST), edit (GET), update (PUT/PATCH), and destroy (DELETE) actions for the model named resource_name. The Rails docs have a much better explanation here: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions.

    (3) There are likely other differences, though it has been a while since I've used Rails 2.x. I'd recommend getting used to the routing schemes for Rails 3+ as they're a bit stricter in how HTTP verbs get defined for your routes.