Edge, RESTful controllers and an admin section. How?

Hi,

Just started using rails, and am using edge. I'd like to have an area (url location) of the site that manages the models (products, product options, categories, orders, content etc.). My first thought (products for example) was to create an AdminProductController controller, that would extend the ProductController that was generated by using scaffold_resource script. Then, in the ProductController I would specify with a filter which actions should be protected. Would doing this, "trick" Rails into getting me the correct views and layouts? It seems backwards to me, to put the authorization in the generated ProductController.

Is it still RESTful to have a url like: /admin/product/1;edit ???

Or do I switch the view and layout and allowed actions based on the user authentication and go with /product/1;edit - for some reason that bothers me?

So... I have the ProductController, a set of public views with a set of actions, a set of admin views and set of actions, and different layouts for each public and admin views. How is this done now with the new REST stuff? Would someone be so kind as to actually post an example? Please? :slight_smile: I'm confused!

Thank you!

- matt

Yes, Virginia, you can have RESTful controllers inside an admin section. First thing is to make sure you’re creating your controllers correctly.

script/generate controller Admin/Products

This will generate Admin::ProductsController. I find it easier to have them all descend from a Admin::CommonController which in turn descends from ActiveRecord::Base, that way they can all inherit admin actions from Admin::CommonController instead of having to add them to each admin controller. Note: Admin/Products needs to be plural because you’re going to end up mapping the products resources in routes.rb as plural. At least, that’s the way it’s set up.

Next, you’ll need your [admin] routes set up like this:

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

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

Notice you have to tell it to use the “admin/products” controller manually. It would look for “products” controller without it. Also, you need to use the :path_prefix to make the route include the initial “/admin” part of the URI. You don’t have to use the :name_prefix but I find it’s handy because it allows you to reuse the named route “products_url” for another [non-admin] controller. However, it does create named routes like the oddly ordered admin_new_product_url. Your call on which way you prefer that.

Hope that helps.

RSL

Ahh, I didn't see this post until now! Thank you very much. I was almost in tears. Where can I find more information on the map object? I don't know what map.with_options does?

- matt

To be honest, I don’t remember where I found the documentation about map.with_options. I know there’s a blog post somewhere out there that I learned more about it but I first found it when I was going through the source code for Mephisto. If you combine the info in the REST-y Rails cheatsheet [ http://nubyonrails.com/articles/2006/10/09/peepcode-rest-basics] and this post, [ http://www.pinupgeek.com/2006/5/24/rails-routing-demystified] you should be able to interpolate the rest. Hope that helps.

RSL

I was like looking for something similar aswell thanks for the tip.

You can also found out about map.with_options here

http://api.rubyonrails.com/classes/ActionController/Routing.html

cheers.