Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails best practice for backend admin system setup?

We have a site where we have a backend management interface, and a frontend that displays our information. We are using Devise to secure authentication.

The backend should allow for normal CRUD type editing of our model objects. The views and layout are also completely different than the frontend. What is the best practice for implementing this in Rails 3?

Our two approaches are:

  1. An admin view folder houses all view specific code, as well as an admin folder in the controllers folder houses all controllers that control admin specific access.
  2. A conditional logic system with one set of views and controllers, with if statements checking whether the user is in admin mode or not.

Which is more recommended, or if there is another approach we have missed, please let me know.

like image 373
bluebit Avatar asked Sep 16 '11 08:09

bluebit


1 Answers

The first solution is better, however for these cases was created the namespaces and the best practice is to go with namespaces when you need relevant differentiation between user site and administration area. Read more about it here

Your directory structure should look like this:

controllers/
     |--admin/
        |--posts_controller.rb

In your routes you put everything you need into admin namespace:

namespace :admin do
  resources :posts, :comments
end

Your controllers should have an admin folder, and a controller in the admin area will look like:

class Admin::PostsController < ApplicationController
end

You also should have an admin folder in your views, where you put the respective views:

views/
   |--admin/
        |--posts/
             |--index.html.erb
             |--...

You can also namespace your models, but it depends on your needs, it is good when you need to have different models with the same name. For example if you need different table for the admin users, and different table for normal users. Personally I wouldn't use model namespacing, just in very justified cases.

The second option I think can cause a lot of headache, you gonna be lost in the if statements, I don't recommend that at all.

like image 82
dombesz Avatar answered Nov 08 '22 00:11

dombesz