Search code examples
ruby-on-railsrubynested-resourcesurl-helper

Rails nested resources and url helpers - how to shorten


In my routes.rb file I have the following resources:

resources :educations do
  resources :semesters do
    resources :module_assignments do
      resources :module_exams do
        resources :module_marks
      end
    end
  end
end

Which generates this url helper:

logonname_module_assignment_module_exams_path   GET /:student/module_assignments/:module_assignment_id/module_exams(.:format)   module_exams#index

Is there any way to shorten this? It should redirect to the same controller and the same action. Instead of

logonname_module_assignment_module_exams_path

I would prefer something like

module_exams_path

Is there a way to solve this? I want all url-helpers like this (index, new, edit, show etc.) not just the show path.


Solution

  • You don't have to nest so deeply.

    I personally only go as far as two deep, it just makes it easier to maintain.

    But that doesn't answer the question. Or maybe it does.

    With your setup. You could do something like:

    match '/:student/module_assignments/:module_assignment_id/module_exams(.:format)' => 'module_exams#index', :as => :module_exams

    This gives you module_exams_path as a helper.