Rails best practice for setting up a backend system?

We have a website that has a backend management interface and an interface that displays our information. We use Devise to provide authentication.

The backend should provide normal CRUD editing for our model objects. The views and layout are also completely different from the look. What is the best practice for implementing this in Rails 3?

Our two approaches:

  • The administrator’s viewing folder contains all the code for viewing, and the administrator’s folder in the controllers folder contains all the controllers that control the administrator’s access.
  • A conditional logical system with one set of views and controllers with if statements that check whether the user is in administrator mode or not.

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

+7
source share
1 answer

The first solution is better, but namespaces was created for these cases, and the best practice is to go with namespaces when you need the appropriate differentiation between the user site and the 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 the admin namespace:

 namespace :admin do resources :posts, :comments end 

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

 class Admin::PostsController < ApplicationController end 

Your views should also have an admin folder where you put the appropriate views:

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

You can also use the namespace for 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 another table for admin users and another table for regular users. Personally, I would not use the model namespace, just in very justified cases.

The second option, which, I think, can cause a lot of headache, you will be lost in if statements, I do not recommend this at all.

+15
source

All Articles