Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rails only name some of my routes?

Here's my routes file

Dumb::Application.routes.draw do

  # an auto-named route
  get '/a/b',            to: 'a#b'

  # apparently not auto-named???
  get '/a/z/:something', to: 'a#z'

end

Here's output of rake routes

a_b GET /a/b(.:format)            a#b
    GET /a/z/:something(.:format) a#z

Wow that sucks! At least for consistency's sake. If I change the a#z route to

get '/a/z/:something', to: 'a#z', as: "a_z"

rake routes will display

a_b GET /a/b(.:format)            a#b
a_z GET /a/z/:something(.:format) a#z

Ok that's good, but having to name the route like that is annoying.

Is this the only solution?

like image 384
Mulan Avatar asked Nov 02 '22 15:11

Mulan


1 Answers

My guess is that Rails can't assign a name to your route because it does not understand it. Usually, you will want to write your route as such :

/a/:id/b/:id  # instead of /a/b/:id which Rails does not understand.

Rails maps a to a controller with a model instance with id :id and b to another controller with another model instance with id :id.

/a/b/:id does not refer to anything in terms of Rails convention.

Getting GET /a/b named to a_b was just a guess Rails made, but it can't work out GET /a/z/:something. What would it be? a_z_something?

like image 183
Justin D. Avatar answered Nov 15 '22 04:11

Justin D.