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:


Admin Controllers/pages:

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

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


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:

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


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

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.


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

Here is some code that should give you some ideas:

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

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

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


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’

Hope this helps,