How to create a separate admin interface for blogs in rails

113 views Asked by At

I need to create some routes for my blog website such that normal user can only read the posts and products. while super admin creates admins to moderate posts. How can i achieve that in rails? I want myapp.com/admin to bring me to log in page for admins. Only super admin can manage moderators.

So far, I have a controller in 'app/controller/admin/home_controller' which has index action for viewing all posts and users for admin.

2

There are 2 answers

0
kajal ojha On

Try adding a before filter before your action so that each time a admin try to hit that url he/she should get redirected to admin_login_url

before_filter :check_role

private
 def check_role
    unless current_user.role=='super_admin'
      redirect_to admin_login_url
    end
  end
0
Ahmed On

I am going to share what i actually did after reading tons of approaches just in case if anyone is stuck where i was. Hopefully helps:

  1. Create a new controller in 'app/controllers/'. This controller is only responsible for GET requests.
  2. Remove Create, Update, Delete methods from the controllers in 'app/controllers/', as these will be only accessible to admins.
  3. Create a new admin_controller.rb in 'app/controllers/admin/'. Add authentications here.
  4. Create new controllers(inherited from AdminsController) per model in the same directory and put the admin operations there.

    class AdminPostsController < AdminsController