Restful routes, nested resources (and /admin)

Hello,

I’m now playing with REST and Rails (with Rails 1.2.1).

I have the following routes in my route.rb :

map.with_options :path_prefix => “/admin”, :name_prefix => “admin_” do |m|

m.resources :requests, :controller => “admin/requests” end

This give me a RESTful /admin/requests controller.

In my application, a Request has many Histories. How can I add the correct route to have something like that :

/admin/requests/24/histories.

Without the /admin, it would simply look like this : map.resources :requests do |request| request.resources :histories end

But with the /admin, everything becomes to be confused for me.

Any help would be greatly appreciated.

Thanks in advance, Thomas.

try this:

map.with_options :path_prefix => "/admin", :name_prefix => "admin_" do |m|   m.resources :requests, :controller => "admin/requests" do |req|     req.resources :histories   end end

ed

Hello Ed, Thanks for your answer.

When I do this : map.with_options :path_prefix => “/admin”, :name_prefix => “admin_” do |m| m.resources :requests, :controller => “admin/requests” do |request|

  request.resources :histories
end

end

And in my view I do this : <%= admin_histories_url %>

I get this error : undefined local variable or method `admin_histories_url’

So I’ve changed to that : map.with_options :path_prefix => “/admin”, :name_prefix => “admin_” do |m| m.resources :histories, :controller => “admin/histories” m.resources :requests, :controller => “admin/requests” do |request| request.resources :histories end end

And now : <%= admin_histories_url %> give me this : http://0.0.0.0:3000/admin/histories

If I try this in my view : <%= admin_histories_url(@request) %>

I get this error : You have a nil object when you didn’t expect it! The error occurred while evaluating nil.to_sym

So I don’t think it solves the problem. Any idea? Thanks, Thomas.

It seems to work with this in my route.rb : map.with_options :path_prefix => “/admin”, :name_prefix => “admin_” do |m| m.resources :requests, :controller => “admin/requests” do |request|

  request.resources :histories, :path_prefix => "/admin/requests/:request_id", :name_prefix => "admin_"
end

end

If I try that in my view : <%= admin_histories_url(@request) %>

I now get this : http://0.0.0.0:3000/admin/requests/24/histories

If anybody wants to comment this solution, he is more than welcome. I’m not sure to do the right thing here.

Thanks, Thomas.

so if you do this: <%= admin_histories_url(@request) %>

you should get something like:   "/admin/requests/1/histories"

if you do this:   <%= admin_histories_url(@request, @history) %>

you should get something like:   "/admin/requests/1/histories/2"

You have achieved what you requested in your original email, no?

ed

Yes, that’s it. But there is still an error in my solution.

Here is the final (I hope) solution : map.with_options :path_prefix => “/admin”, :name_prefix => “admin_” do |m| m.resources :requests, :controller => “admin/requests” do |request| request.resources :histories, :path_prefix => “/admin/requests/:request_id”, :name_prefix => “admin_”, :controller => “admin/histories”

end

end

In my previous solution, I forgot ":controller => “admin/histories”. Thanks for your help.

Everybody is still welcome to comment this solution. Thomas.

I ran across this the other day and was confused about why the with_options options don’t trickle down but I guess it doesn’t actually matter why since they don’t.

RSL