Current verbose rails path helpers
I’m constantly writing code to get URLs like:
link_to @applicant.name, company_job_applicant_path(@company, @job, @applicant)
However this code looks more like this (redundant) piece:
link_to @applicant.name, company_job_applicant_path(@applicant.job.company, @applicant.job, @applicant)
This is silly.
Required ‘pert’ path helpers
The other parameters can clearly be derived from the @job. All I should really need to type is:
link_to @applicant.name, applicant_quick_path @applicant
where there is a definition somewhere of:
def applicant_quick_path applicant
company_job_applicant_path(applicant.job.company, applicant.job, applicant)
end
My questions
- Is this a reasonable
Rails Wayto do things - Where should I store this method?
- I can currently access these helpers in the console using
app.company_path. How would I access my new helper methods from the console?
Yes, DRY is the “Rails way” to do things. If you’re repeating this method over and over again, it makes sense to create a view helper for it. Instead of modifying the path helpers, I’d simply wrap rails
link_tomethod.You can do something quick and easy like this:
Alternatively, you can roll in some extra support for the
link_tomethodIf you want to fully support all the features provided by
link_to, you can see how they permit for multiple function signatures hereRSpec notes
If you’d like to write tests for your view helpers in RSpec, follow this guide:
https://www.relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec