Greetings everyone,
I've the following:
routes.rb
:
namespace :hr do
resources :employees do
resources :skills
end
end
And those models:
hr/skills.rb
class Hr::Skill < ApplicationRecord
end
hr/employees.rb
:
class Hr::Employee < ApplicationRecord
end
I'm trying to make #url_for
resolve to hr_employee_skills_path(@employee, @skill)
. I need #url_for
to work that way because it's internally used by #simple_form_for
within SimpleForm.
I tried different combinations of #url_for
to give me the desired url path generator but none worked:
url_for [Hr::Employee.new, Hr::Skill.new]
NoMethodError: undefined method `hr_employee_hr_skills_url' for main:Object
Also this:
url_for [:hr, Hr::Employee.new, Hr::Skill.new]
NoMethodError: undefined method `hr_hr_employee_hr_skills_url' for main:Object
I just need #url_for
to call hr_employee_skills_path(employee, skill)
so that it resolves to the actual route. How could that be possible?
Check out the link, but there is an option for the module that you can use to help with this:
module Hr
def self.use_relative_model_naming?
true
end
end
This then allows for this:
url_for([:hr, Hr::Employee.first, Hr::Skill.first, only_path: true])
=> "/hr/employees/1/skills/1"
https://coderwall.com/p/heed_q/rails-routing-and-namespaced-models