简体   繁体   English

多种布局设计

[英]devise with multiple layout

I really like to authenticate my devise user through 2 different interfaces with a view to have 2 different layout. 我真的很想通过2个不同的界面对我的设计用户进行身份验证,以具有2个不同的布局。

For example I would be able to use /users/sign_in and /admin/sign_in based on the same User model. 例如,我将能够基于相同的用户模型使用/ users / sign_in和/ admin / sign_in。

I had set 2 routes : 我设置了2条路线:

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: 但是我不知道这样做是正确的方法,因为我需要在应用程序控制器上覆盖current_user,如下所示:

def current_user
    super || current_admin_user
end

Moreover I have 2 methods : authenticate_user! 而且我有2种方法:authenticate_user! and authenticate_admin_user! 和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. 无需覆盖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. current_user将返回已登录的current_user,无论是管理员还是不是管理员。 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! 如果您希望用户只有普通用户才能以管理员身份登录,则建议使用另一种方法:为管理员创建另一个模型并为require_user过滤! for the admin sign_in . 用于admin sign_in

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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