简体   繁体   中英

devise with multiple layout

I really like to authenticate my devise user through 2 different interfaces with a view to have 2 different layout.

For example I would be able to use /users/sign_in and /admin/sign_in based on the same User model.

I had set 2 routes :

devise_for :users

and

devise_for :users, :module => "admin/users", :path => ''

But I'm not sur it's the right way to do that because I need overwrite current_user on my application controller, like this:

def current_user
    super || current_admin_user
end

Moreover I have 2 methods : authenticate_user! and authenticate_admin_user!

I'm really confused with this specification, can anybody help ?

I have a different controller admin in that i have added a login action.

 class AdminController < ApplicationController   
    def login
      @user = User.new
    end
  end

In view of login.html.erb

<%= form_for(@user, :as => :user, :url => session_path(:user)) do |f| %>
<% end %>


U can now call admin/login path

 and successfully got sign up, but if you want to redirect to some other page after sign up instead of root url then

In application controller write inside this method of devise

def after_sign_in_path_for(resource)
 end

I'm not sure if I got your problem, if not, please comment on it :)

There is no need to overwrite current_user. You can create a filter that filters admins like this:

def require_admin_user
  unless current_user.admin
    flash[:error] = "You need admin privileges to enter this area"
    redirect_to root_path 
  end
end

current_user will return the current_user logged in, whether it is an admin or it isn't an admin. If you want for an user to be able to login in as an admin only if they as an ordinary user, I would suggest another approach: creating another model for admins and filtering for require_user! for the admin sign_in .

最好的选择是使用STA(单表继承)…然后可以使用2个devise_for声明,每个模型一个。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM