In Rails 3.2 I am using these routes declarations:
get 'contact' => 'contact#new', :as => 'contact'
post 'contact' => 'contact#create', :as => 'contact'
They result in (rake routes
):
contact_en GET /en/contact(.:format) contact#new {:locale=>"en"}
contact_de GET /de/kontakt(.:format) contact#new {:locale=>"de"}
contact_en POST /en/contact(.:format) contact#create {:locale=>"en"}
contact_de POST /de/kontakt(.:format) contact#create {:locale=>"de"}
Now Rails 4.0 complains about this configuration:
Invalid route name, already in use: 'contact' You may have defined two routes with the same name using the
:as
option, or you may be overriding a route already defined by a resource with the same naming.
Obviously the routes share the same name, but as the request types differ, I'd expect them to be accepted as before.
How can I tell Rails 4 to generate the routes just like before in 3.2?
In your situation simply not specifying the :as
option is sufficient as Rails will automatically get the route name from the path:
get 'contact' => 'contact#new'
post 'contact' => 'contact#create'
However if you have a more complex path pattern or want to refer to the route with a different name then you should specifically set the second route to :as => nil
(or as: nil
using the new hash syntax).
So if you wanted to name the route as person_path
you would need to do:
get 'contact' => 'contact#new', :as => 'person'
post 'contact' => 'contact#create', :as => nil
If these two routes have the same URL, you don't need to name the second one. So the following should work:
get 'contact' => 'contact#new', :as => 'contact'
post 'contact' => 'contact#create'
Why do you use the :as
? It seems to be not needed in this case.
get 'contact' => 'contact#new'
post 'contact' => 'contact#create'
gives
Prefix Verb URI Pattern Controller#Action
contact GET /contact(.:format) contact#new
POST /contact(.:format) contact#create
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With