admin vs public. multiple views needed?

i have a newbie question. been playing around with RoR for a while now, but finally building an application for public usage. my question concerns how to create an admin section and a public section of the website/application. for instance, i want an admin section which lets the owners of the site log and make changes. i'm wondering do i need multiple controllers with authentication turned on (i'm using a standard example, which uses a before_filter on controllers that require login) for admin and separate controllers to be used by the public pages? i'm not sure if i'm explaining this correctly. here's a list of things i'm trying to accomplish:

Models: food categories events

Admin Controllers/pages: food categories events

public pages (separate controllers and views needed?: food (sorted by categories) events

and if i do need separate controllers and views, do i link them to the shared models using custom Routes?

sorry if this is confusing. thanks

joe

The answer is: you can do anything you want :slight_smile:

But new in Rails 2.0 is Namespaces in Routes, there are a few primers on what there are and how to use them, I found this on Google, should get you started: http://railstips.org/2007/4/28/namespaces-added-to-routes

The other option is to use in-place editing, where the actual option to click on a field and edit it only exists if you're an admin/authorized. It actually cleans up controller code a bit. That's how I solved this problem.

thanks for the info. i'll check out the namespaces link.

The third option is to use different layouts and templates based on if they are logged in. This lets you completely customize the look for admins while maintaining basically the same controllers.

I haven't made up my mind on which way is best.

that's probably more what i was thinking. so where do you do the check for login? in the controller for each model/view? right now, i'm doing a before_filter to call my authenticate action. i'm thinking i have to move that somewhere else? how would i go about that? thanks

joe

in the respond_to block. I saw it in the Peepcode video on Restful development.

Here is some code that should give you some ideas:

      format.html do         render(:layout => "admin") if logged_in?       end

I haven't tried this yet but I think you can also do something like this:

      format.html do         render(:layout => "admin", :template => "admin_index") if logged_in?       end

Oh yea, you'd also probably want to restrict access to some of the actions using:

  before_filter :login_required, :except => [:index, :show, :home]

These are all cut/paste from the peepcode code. I highly recommend them. For $9, its the best training on rails available... either than RailsCasts.com

yeah, definitely been digging PeepCode thus far.

so i figured out what i really wanted to do. i've been stuck in the 1 Model : 1 Controller : 1 View mindset. when today, i thought, why not create a different controller and view pair for each public website section i want. is this frowned upon or a perk of the MVC pattern?

thanks to all for the help.

joe

I'd highly recommend using namespaces so you can have a separate controller/views. For the layout (I use just a single admin layout), create a switch method in your application controller:

layout :set_layout

def set_layout   params[:controller] =~ /^admin/ ? 'admin' : 'public' end

Hope this helps, Matt