Search code examples
ruby-on-railsrubyrubygemscontactus

Rails 4.0.x how to route the root action of contact_us gem to specified action?


I'm using contact_us gem version 0.5.4

I've following code in my routes.rb file

resources :contacts, controller: 'contact_us', only: [:new, :create] do
  root :to => 'contact_us#new'
end

in my understanding the above route for contacts will only support :new and :create actions, and with specified controller controller: 'contact_us' also it with root / it will redirect to #new action but when I hit http://localhost:3000/contact-us in my browser it says

Unknown action
The action 'index' could not be found for ContactUsController

I've upgraded the rails version from 3.2.19 to 4.0.13 and ruby to 2.0.0p481

the old code was working fine with rails 3.2.19 and ruby 1.8.7

resources :contacts,
  :controller => 'contact_us',
  :only       => [:new, :create]
match 'contact_us' => 'contact_us#new'

if I only change match with get in above code it throws this error

/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/actionpack-4.0.13/lib/action_dispatch/routing/route_set.rb:430:in `add_route': Invalid route name, already in use: 'contact_us' (ArgumentError)

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. For the latter, you can restrict the routes created with resources as explained here:


Solution

  • adding :as in the route does the jobs

    resources :contacts,
      :controller => 'contact_us',
      :only       => [:new, :create]
    get 'contact_us' => 'contact_us#new', as: :contact_us2
    

    as identified by Albin in the chat the contact_us modules route file it already has same route but with different alias

    get "contact-us" => "contact_us/contacts#new", as: :contact_us #line#11
    

    I just added same route with different path and different alias,