简体   繁体   中英

How to suppress Rails's auto naming routes?

Starting form 3, Rails will automatically name the following route

match 'list/my/pets", :to => 'pets_controller#cat'

with list_my_pets .

How can I get rid of this name?

It could collide with some other named routes which I define later and happen to have the name.

For example, I may define:

scope :controller => :company do
  match 'groups', :action => :groups  # I don't want it to be named 'groups' !
  ...and a lot more ...
end

and later I define:

resources :groups

whose named routes I really need.

Of course, changing the order works, but then we have to be very careful with the order.

Isn't there a way to explicitly suppress the naming?

If you want to effectively "unname" the route. Use as: nil

You can override the default with as: ( documentation ). Eg:

get 'list/my/pets' => 'pets#cat', as: :whatever_alias_i_want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM