Topic: RESTful Admin Layout

I am usually the first to just use one layout for all my models, selectively hiding certain elements with before filters. This works well for most situations. Right now however, I am dealing with a unique problem, where the admin needs to have a completely different layout from the public side.

Organizationally speaking, what is the best way to accomplish this RESTfully? I was thinking of using an admin controller and nesting the other controllers below it but I'm not convinced that it's the cleanest solution.

I have about 10 different CRUD models and the public can only access the view and index actions.

Ideas?

Re: RESTful Admin Layout

can't you just programatically select which layout to use dependent on which type of user is logged in

maybe use a before filter and define a method to determine which layout to use.

Re: RESTful Admin Layout

Smart lad!

I had not even considered that option, but it makes the most sense. I already have user info in my application controller so without much bother i can say

if current_user.admin
  render :action => 'index', :layout => 'admin'
else
  render :action => 'index', :layout => 'public'
end

Gonna test this out tomorrow. Thanks for the nudge!

Re: RESTful Admin Layout

You can do this in the controller to dynamically select the layout for multiple actions:

layout :user_layout

def user_layout
  if current_user.admin
    'admin'
  else
    'public'
  end
end

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful Admin Layout

I just hit a little snag on this one. It works well for the admin side, but I need the admin to have the option to also view the public index. As it stands, the admin would have to log out...not too user-friendly.

I am thinking that perhaps I need to create an additional protected action like admin_index... The only problem I forsee is that I will lose the RESTful route of models_path.

At this stage it seems like my best bet

Re: RESTful Admin Layout

I recommend using a subdomain instead (admin.example.com). You can check the subdomain in the controller and only return the admin stuff if it's the admin subdirectly. It basically goes to the exact same site, just with the difference of hiding/showing the admin stuff.

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful Admin Layout

ryanb - it's like you're living in my mind..

Re: RESTful Admin Layout

Sigh... yet another thing to research..

But you know what- I was planning on using a subdomain to deliver protected downloads anyway, so I suppose this is as good a time as any to get familiar with subdomain voodoo.

Re: RESTful Admin Layout

So I'm doing some homework on this and came across an interesting approach by Jamis Buck:

http://weblog.jamisbuck.org/2006/10/26/ … g-routes-2

This looks like it could work, but Jamis suggests using an admin controller which brings me back to square one.. I much prefer your idea of using the same exact controllers, routing them to the subdomain an setting layout dynamically. I'm just not sure how to proceed. Perhaps this could be a new Railscast? smile

Re: RESTful Admin Layout

The article shows you how to change the routes based on the subdomain, but you don't need to do this. The admin subdomain should go to exactly the same places as the public one so you shouldn't need to change your routes at all. What you need to do is set up your name server and web server to properly point to Rails when accessing the admin subdomain. Then it's a matter of checking if the admin subdomain is in the request before displaying the admin. For example:

# in application.rb
def admin?
  if request.subdomains.include? 'admin' # only consider admin if they are in the admin subdomain
    # add other admin conditions
  end
end

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful Admin Layout

What you need to do is set up your name server and web server to properly point to Rails when accessing the admin subdomain.

Since I have yet to deploy, I'm not sure how to do this. I imagine that I would need to configure nginx somehow. Perhaps this is a question better answered on the Slicehost forums.

Re: RESTful Admin Layout

Yeah, I'm not sure how to do it in nginx. You can do this if you only want that requirement to take place in production environment so you can see the admin stuff in development on your local:

def admin?
  if request.subdomains.include?('admin') || ENV['RAILS_ENV'] != 'production'
    # add other admin conditions
  end
end

Railscasts - Free Ruby on Rails Screencasts

Re: RESTful Admin Layout

I was trying to get a good set up and found this on web -> Creating a RESTful admin section in Rails = http://www.fallenrogue.com/articles/178 … n-in-Rails
Maybe can help

Re: RESTful Admin Layout

Awesome article and great first post.

Welcome to the forums!

smile