簡體   English   中英

多種布局設計

[英]devise with multiple layout

我真的很想通過2個不同的界面對我的設計用戶進行身份驗證,以具有2個不同的布局。

例如,我將能夠基於相同的用戶模型使用/ users / sign_in和/ admin / sign_in。

我設置了2條路線:

devise_for :users

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

但是我不知道這樣做是正確的方法,因為我需要在應用程序控制器上覆蓋current_user,如下所示:

def current_user
    super || current_admin_user
end

而且我有2種方法:authenticate_user! 和authenticate_admin_user!

我真的對這個規范感到困惑,有人可以幫忙嗎?

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

我不確定是否遇到您的問題,如果沒有,請對此發表評論:)

無需覆蓋current_user。 您可以創建一個過濾器來過濾管理員,如下所示:

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將返回已登錄的current_user,無論是管理員還是不是管理員。 如果您希望用戶只有普通用戶才能以管理員身份登錄,則建議使用另一種方法:為管理員創建另一個模型並為require_user過濾! 用於admin sign_in

最好的選擇是使用STA(單表繼承)…然后可以使用2個devise_for聲明,每個模型一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM