I have a Project model that has_many :workers. In routes.rb, workers are a nested resource for projects:
resources :projects do
resources :workers
end
I would like to make it so that when a user goes to the path
'/project_name'
they’re routed to the show action for the project with that name.
And when a user goes to the path
'/project_name/workers/worker_id'
they’re routed to the show action for the worker with that id.
I would also like to make it so that project_worker_path(@project, @worker) routes to
'/project_name/workers/worker_id'
I’d appreciate any help.
Haven’t tested it, but I believe the following will work.
In routes.rb:
In project.rb:
In projects_controller.rb:
In workers_controller.rb:
Basically,
to_paramis used to generate the url, and:path => '/'tells it that you don’t want anything to come before the parameter. This will also map the root path toprojects#indexif that’s what you want it to do. You can turn that part off by passing:except => :indextoresources.