# Admin vs public. multiple views needed?

**URL:** <https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237>\
**Category:** Rails\
**Created:** [February 6, 2008, 3:01am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237 "2008-02-06T03:01:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 3:01am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/1 "2008-02-06T03:01:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 3:15am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/2 "2008-02-06T03:15:54Z")

</div>

The answer is: you can do anything you want 🙂

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](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.

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 6:41am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/3 "2008-02-06T06:41:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 6:47am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/4 "2008-02-06T06:47:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 8:17am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/5 "2008-02-06T08:17:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 8:53am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/6 "2008-02-06T08:53:20Z")

</div>

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

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 7, 2008, 2:27am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/7 "2008-02-07T02:27:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 6, 2008, 8:49am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/8 "2008-02-06T08:49:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarbonJoeTunis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@CarbonJoeTunis](https://www.ruby-forum.com/u/CarbonJoeTunis)\
**Post date:** [February 7, 2008, 4:56am UTC](https://www.ruby-forum.com/t/admin-vs-public-multiple-views-needed/128237/9 "2008-02-07T04:56:31Z")

</div>

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
