0

I have a controller mounted at the root of an Engine with the same name

module Contacts
  class ContactsController < ApplicationController
   ...
  end
end

Contacts::Engine.routes.draw do
  root 'contacts#index'
  resources :contacts, :path => '/'    
end

So I get the expected routes:

         root GET    /                   contacts/contacts#index
     contacts GET    /                   contacts/contacts#index
              POST   /                   contacts/contacts#create
  new_contact GET    /new(.:format)      contacts/contacts#new
 edit_contact GET    /:id/edit(.:format) contacts/contacts#edit
      contact GET    /:id(.:format)      contacts/contacts#show

However I have a small problem with my Model:

module Contacts
  class Contact
    include ActiveModel::Model 
    include ActiveModel::Serialization
    ...
  end
end

Looks like my link_to insists in using the false url_path, and I am not sure where I can fix the relationship between my model and the url helpers of my controller:

link_to 'Edit', [:edit, @contact]
NoMethodError: undefined method `edit_contacts_path' for #<#<Class:0x007fc421e17e08>:0x007fc41ca4c8f0>

Why is the plural form for the :edit path being used?

SystematicFrank
  • 16,555
  • 7
  • 56
  • 102

1 Answers1

2

link_to 'Edit', [:edit, @contact] is incorrect. Rails detects the action from the state of the @object. What's happening is that Rails interprets :edit as a namespace.

link_to 'Edit', [:edit, @contact]

See the polymorphic documentation.

I'm not sure you can generate an edit polymorphic route in that way. And if you can, :edit should be after the resource. Example (I haven't tried it)

link_to 'Edit', [@contact, :edit]

I suggest you to use the explicit route name

link_to edit_contact_path(@contact)

I don't see any advantage in your case to use the polymorphic route.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364