Given Rails 4.1, two controllers CRM::CompaniesController and CRM::ContactPeople, two models CRM::Company and CRM::ContactPerson and the following routes:
namespace :crm do
resources :companies do
resources :contact_people
end
end
The generated URL helpers contain the CRM namespace only once, which is exactly what I want:
crm_company_contact_people GET /crm/companies/:company_id/contact_people(.:format)
new_crm_company_contact_person GET /crm/companies/:company_id/contact_people/new(.:format)
# ...
However, using the Array approach for URL helpers
= form_for([@crm_company, @crm_contact_person]) do |f|
tries to generate a URL with each resource namespaced:
undefined method `crm_company_crm_contact_people_path' for #<#<Class...
I would like to have "crm" in my paths only once at the beginning (unless it breaks a common Rails approach) and it would be ugly to add the URL to each form explicitly. Is there something I can do (perhaps in the routes, the model or the first form_for argument) so that Rails knows how to build the correct path? Or is there a more Rails-like way to generate this kind of structure so that Rails knows automatically how to build the paths?
You need to include the namespace in your call to form_for:
= form_for([:crm, @crm_company, @crm_contact_person]) do |f|
Also, since you're using namespaced models, you need to define the following class method on them (from rails/rails issue #10705) to ignore the namespace:
def self.use_relative_model_naming?
true
end
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