So here’s the routes in question:
resources :subjects, path: 'library' do
resources :modules, controller: 'subject_modules'
end
When I write form_for [@subject, @subject_module] (with those two set to what you’d expect), Rails tries to generate “subject_subject_module_path”.
When I remove the :path for the subjects resource, the generated helper remains the same (as expected).
What it should be is “subject_module_path”; I suspect the problem is that Rails looks at the controller for the modules resources and uses that instead of its actual path, i.e. it builds this helper:
subject_ + subject_module_ + path
From what I’ve gathered so far, it’s pointing towards the possibility of a bug, but is it possible it’s more something on my side or something intended by design?
For now, I suppose this is usable as a temporary fix:
form_for([@subject, @subject_module], url: subject_module_path(@subject, @subject_module))
I’m using Rails 3.1.3.
This is something that occurs due to the design of Rails.
The following is what the Rails API docs say in relation to
url_for:But saying explicitly declaring a helper is required when you have a “nested route” is in accurate. If the nesting occurs under a resource (without certain kinds of routing options, more on this later), Rails will be able to generate the path without any issues.
form_forappears to at some point, likeurl_for, callpolymorphic_urlin order to generate the URL to target, which in turn callsbuild_named_route_call.You can see that
build_named_route_callsimply generates underscored_versions of the ModelNames passed in, and joins them together with underscores.Bringing that back to my routing:
Since the subjects resource is under /library/ by setting
:path, its helpers remain assubjects_*, and thus Rails has no problems generating a URL for it when passed aSubject. Thesubject_modulesresource (named as such since Rails reserves the name Module for models) however, has had its named helpers changed from its model name through the setting of:controller.Mystery solved.