I am trying to get the following routes to work in my Rails 3 app.
Scenario:
I have the following controllers in my app:- Practices
- Doctors
- Patients
- Prescriptions
Putting up :resources for each of them in routes.rb gives me routes from
- example.com/practices
- example.com/doctors/1/edit etc
What I'd like to have however is the fo开发者_JAVA百科llowing resourceful routes, for example:
- example.com/james_practice/docs that translates to the doctors controller
- example.com/james_practice/awesome_prescriptions that routes to the prescriptions controller etc. etc. both of these giving me access to :practice name and furthermore route the correct controller with all the helpers like edit_docs_path(doctor) etc.
How would I go about this? I've used
resources :prescriptions, :path => "/:practice/awesome_prescriptions"
but although it showed the correct routes in "rake routes", it still wouldn't work as expected.
I think this is the route you're looking for:
scope :path => ":practice" do
resources :docs, :controller => "doctors"
resources :awesome_prescriptions, :controller => "prescriptions"
end
By the way, you didn't give me the example of Patients
, so I didn't put it there.
map.resources is just a pattern, more like a shortcut. If you want to have URLs like in your examples you should use "named routes". A good screencast for Rails 2.x: http://railscasts.com/episodes/34-named-routes
Edit: Read section 3.2 in Rails Tutorial: http://guides.rubyonrails.org/routing.html
p.s. Also you'll face a case where people use "." in their names and it'll cause problems. Here's my route for "tags"
map.resources :tags, :requirements => { :id => %r([^/;,?]+) }
精彩评论