N
N
Nikita2016-07-18 14:23:07
Ruby on Rails
Nikita, 2016-07-18 14:23:07

Rails - devise how to redirect from controller if user is on admin?

Hello!
It is necessary to redirect the user from the controller if he is not authorized and not an admin.
I do it like this:

class AdminController < ApplicationController
  before_filter :authenticate_user!

  def home
  	unless @current_user.admin?
  		redirect_to ###
  	end
  end
end

Yes, it works, but writing to each method is not entirely rational. I'm sure there is a more convenient way.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2016-07-18
@niks123123

I found a solution for myself like this:

class AdminController < ApplicationController
  before_filter :authenticate_user!
  before_filter do
  	redirect_to some_path unless current_user.admin?
  end

  def home
  end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question