RESTful admin controllers and views with Rails

You want a RESTful Rails app with a backend administration? The worst thing to do in my opinion is to use the same controllers for the public and the admin side. At first, it might look wise to do this. I mean, if you have a “books” resource, it would be logical that all methods related to books go in the same controller, right? Well, logical or not… I suggest you never do this because your application will become a real mess in no time. Personally, I did it once and will never get caught again!
Exemple of a mess in a controller :

def index
  if administrator?
    @books = Book.all
  else
    @books = Book.published
  end
end

Example of a mess in a view :

<%if administrator?%>
  <%=render partial: "admin_index"%>
<%else%>
  

Published books

bla bla bla <%end%>

If you go that route, be prepared to make your fingers bleed because you will write tons of confusing and ugly “if” statements everywhere. Most of the time anyway, what you need to do with the resources is completely different depending if you’re on the admin or public side, so you’re better to separate them.
Step #1 : Generating the controllers
To generate a public controller, you do like you always do : ./script/generate controller books
To generate its admin counterpart, you simply do this : ./script/generate controller admin/books
Rails will generate the controller in controllers/admin/books_controller.rb and a folder for the views in views/admin/books
Step #2 : Configuring the routes
One route for the public side :

map.resources :books, :only => [:index, :show]

One route for the admin side

map.namespace :admin do |admin|
  admin.resources :books
  admin.resources :some_other_resource
end

Now your namespaced controller has its own named urls as well : admin_books_url, edit_admin_book_url and so on…
Step #3 : Get the “form_for” url right

<%form_for [:admin, @book] do |f|%>
   <%=f.text_field :title%>
   <%=f.submit "save"%>
<%end%>

That way Rails will correctly call the update/create method in controllers/admin/books_controller.rb instead of the one in controllers/books_controller.rb
A final note
The controllers and the views are best kept separated but NOT the model which should always remain unique in your app.

19 thoughts on “RESTful admin controllers and views with Rails

  1. @mike wyatt
    Yes, this is without a doubt a more elegant way to fetch the resources. But what about before and after filters that must not be called when you’re on the admin side or the other way around? It means more conditionals expressions and more obstrusive code to write. And then there are the update and create actions that will differ and generate some “if” statements… and then there are the views, the partials… Exceptions start to popup everywhere and the nightmare begins!
    Thanks for your code snippet though. I like how you made it generic and named the method “resources”. It keeps you thinking in REST mode.

  2. I have been using this technique for a while now on a very large app. I not only have admins and normal users, but some in between types. The namespacing works great to separate out these concerns. My only complaint with the technique is that views tend to not be dry, so sometimes a single UI change might require editing multiple views across all of the namespaces.

  3. @Brandon Hauff
    Very good point. But IMO it is a scenario where it’s ok to slack off with the DRY principle. It’s better to repeat a few things here and there with your views than to pollute your app with some ugly conditional statements.
    Thanks for your comment!

  4. It’s unbelievable how this post helped me in the last project I’m working on. I avoid complex structures of ifs everywhere… even if sometimes, I have to repeat myself, it’s much clearer.

  5. @Dan, glad that you found the post useful!
    Instead of DRY, it should be TNRYBDBAAI : Try Not Repeating Yourself But Don’t Be Anal About It.

  6. Just to put my 2 cents in. Most of the time administrative views are completely different to what normal users see. Take WordPress for example, there’s little to no “repeating yourself”! But, if you don’t have a CMS then use partials.

  7. This might be a stupid question, but what would you do for the create action in the admin#books controller? For example, the following would not work: format.html { redirect_to @book, notice: ‘Book was successfully created.’ }?

  8. With havin so much content do you ever run into any issues of plagorism or copyright violation? My blog has a lot
    of exclusive content I’ve either authored myself or outsourced but it seems
    a lot of it is popping it up all over the
    web without my permission. Do you know any ways to help reduce content from being ripped off?
    I’d truly appreciate it.

  9. I was wondering if you ever thought of changing the page
    layout of your site? Its very well written; I love
    what youve got to say. But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or 2 pictures.
    Maybe you could space it out better?

Leave a Reply to Sam Kinesto Cancel reply

Your email address will not be published. Required fields are marked *