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.
dombesz
source share