2011-12-02 52 views

回答

65

是的,你能做到這一點,當running the generator跳過用戶模型創建:

rails generate active_admin:install --skip-users 

然後在您的config/initializers/active_admin.rb

# == User Authentication 
# 
# Active Admin will automatically call an authentication 
# method in a before filter of all controller actions to 
# ensure that there is a currently logged in admin user. 
# 
# This setting changes the method which Active Admin calls 
# within the controller. 
config.authentication_method = :authenticate_admin! 

取消註釋config.authentication_method併爲您的管理提供您的身份驗證方法,例如:

# app/controllers/application_controller.rb 
def authenticate_admin! 
redirect_to new_user_session_path unless current_user.is_admin? 
end 

重新啓動服務器,它應該是工作。也看看Active Admin Configuration

希望這會有所幫助。

+8

哪兒你放置authenticate_admin!方法?我試過了應用程序控制器,但是得到: 未定義的方法'authenticate_admin_user!' for#

+1

你可以將它放在config/initializers/active_admin.rb文件中。 – jackyalcine

+0

或在應用程序控制器中。 – domachine

24

如前所述,您需要更新您的config/initializers/active_admin.rb以反映正確的auth方法。

此外,然而,你將要更新以下設置,以及:

# This setting changes the method which Active Admin calls 
# to return the currently logged in user. 
config.current_user_method = :current_admin_user 

config.current_user_method = :current_user 

# This setting changes the path where the link points to. If it's 
# a string, the strings is used as the path. If it's a Symbol, we 
# will call the method to return the path. 
# 
# Default: 
config.logout_link_path = :destroy_admin_user_session_path 

config.logout_link_path = :destroy_user_session_path 

當然,您不必更新這些(或後面提到的方法),並且只是在其他地方重新使用這些方法,但這似乎是最簡單/最乾淨的方法。您顯然需要使用設計認證將每個設置(current_USER)中的「用戶」替換爲模型的名稱。

我也建議更新,而你在那裏以下設置,以及:

# This setting changes the http method used when rendering the 
# link. For example :get, :delete, :put, etc.. 
# 
# Default: 
config.logout_link_method = :get 

config.logout_link_method = :delete 

需要這最後的變化,如果使用的色器件配置默認的HTTP方法設置爲:delete,除非你改變它。重要的是,它們現在已同步,因爲如果按照這些說明進行操作,則將使用destroy_user_session_path,這是由設計人員定義的路徑。否則,您會收到一條消息,指出[GET]/users/sign_out路由不存在。

+0

如果您不會使用current_user更新current_user_method,則您可能會看到一個閃爍的「未保存評論,文本爲空」。在創建ActiveAdmin註釋時,[當前實現僅在失敗時提供empty_text錯誤](https://github.com/activeadmin/activeadmin/blob/50d9893ea60993ef72047f072e5a4bc0c4d24b45/lib/active_admin/orm/active_record/comments.rb# L59)。 – rakvium

4

所有與 http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin-and-devise/

中規定的指南,增加了對信息的一些額外位,如果你選擇恢復到的選項,一個用戶一起的其他人都已經表示,以及模型,當你已經實現了admin_user模型(即現在你有'用戶'以及'admin_user'模型)。

額外的步驟包括

從routes.rb中 複製代碼app/admin/admin_user.rb刪除devise_for :admin_users, ActiveAdmin::Devise.configapp/admin/user.rb(只使用需要什麼) 刪除app/admin/admin_user.rb(或者你會得到一個Uninitialized constant error on AdminUser)這樣的傢伙(和我以及)。

3

這裏,如果你已經使用默認設置安裝ActiveAdmin的過程中,要對現有的模型進行驗證與User.is_admin外地用戶,並刪除admin_user表:

回滾admin_user遷移(如果你沒有使用--skip-users安裝Active管理員時):

rake db:migrate:down VERSION=20141205110842 # create_active_admin_comments.rb 
rake db:migrate:down VERSION=20141205110831 # add_devise_to_admin_users.rb 
rake db:migrate:down VERSION=20141205110820 # devise_create_admin_users.rb 

然後刪除那些3個文件。

在路由,刪除行devise_for :admin_users, ActiveAdmin::Devise.config

在application_controller.rb,添加:

def authenticate_admin! 
    if current_user && current_user.is_admin 
    # fine 
    else 
    redirect_to new_user_session_path 
    end 
end 

在active_admin.rb:

config.authentication_method = :authenticate_admin! 
config.current_user_method = :current_user 
config.logout_link_path = :destroy_user_session_path 
config.allow_comments = false 
config.logout_link_method = :get # couldn't get active_admin to sign out via :delete. So I configure devise to sign out via :get. 

要配置色器件登出經由:get, add in devise.rb:

config.sign_out_via = :get 
# And for every occurrence of destroy_user_session_path, remove the option method: delete. 

創建is_admin遷移:

rails g migration add_is_admin_to_user is_admin:boolean 

編輯遷移像這樣:

class AddIsAdminToUser < ActiveRecord::Migration 
    def change 
    add_column :users, :is_admin, :boolean, default: false 
    end 
end 

和遷移:

rake db:migrate 

如果在軌道4,不要忘了添加is_admin在permit_params。在應用程序/管理/ user.rb:

permit_params ....., :is_admin 

添加權管理員用戶,在控制檯:

u = User.find(42); u.is_admin = true; u.save 

享受

相關問題