Can アクティブな管理者私の現在の使用Deviseのユーザーモデルですか?既にadmin
という名前の列があり、true
の場合は、/admin
に行くときにアクティブな管理者ログインをバイパスしたいと思います。Rails 3 - Active_adminは既存のユーザーモデルを使用できますか?
これは可能ですか?
現在の路線:
残りの部分は基本的に標準の工夫+アクティブadminです
Can アクティブな管理者私の現在の使用Deviseのユーザーモデルですか?既にadmin
という名前の列があり、true
の場合は、/admin
に行くときにアクティブな管理者ログインをバイパスしたいと思います。Rails 3 - Active_adminは既存のユーザーモデルを使用できますか?
これは可能ですか?
現在の路線:
残りの部分は基本的に標準の工夫+アクティブadminです
はいあなたは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
これが役に立ちます。
前述のとおり、config/initializers/active_admin.rb
に正しい認証方法が反映されるように更新する必要があります。
また、しかし、あなたにも、以下の設定を更新することになるでしょう:
config.logout_link_path = :destroy_user_session_path
へ
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
へ
# This setting changes the method which Active Admin calls
# to return the currently logged in user.
config.current_user_method = :current_admin_user
を
もちろん、これらの(または記事に記載されている)メソッドを更新する必要はなく、別の方法でメソッドをオーバーライドしても問題ありませんが、これは最も簡単できれいなアプローチです。明らかに、各設定(current_USER
)の "user"を、devise認証を使用するモデルの名前に置き換える必要があります。
私はまた、あなたがそこにいる間にも次の設定を更新し推薦する:
# 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
に設定されています(変更しない限り)。これらの手順に従うと、すでにdeviseによって定義されているパスである
destroy_user_session_path
が使用されるため、現在同期されていることが重要です。そうしないと、[GET]/users/sign_outルートが存在しないというメッセージが表示されます。
current_user_methodをcurrent_userで更新しないと、「コメントは保存されていません、テキストは空です」というフラッシュが表示されます。 ActiveAdminのコメント作成では、[現在の実装では障害発生時にempty_textエラーのみが提供されます。](https://github.com/activeadmin/activeadmin/blob/50d9893ea60993ef72047f072e5a4bc0c4d24b45/lib/active_admin/orm/active_record/comments.rb# L59)。 – rakvium
誰もがあなたが単一のユーザーを持っているオプションに戻す選択している場合は情報のいくつかの追加ビットを付加 http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin-and-devise/
でレイアウトガイドと一緒にだけでなく、言ったことのすべてあなたが既にadmin_userモデルを実装しているとき(つまり、今は 'user'と 'admin_user'モデルを持っている)、モデルを作成します。
追加の手順が
を含め、この男が持っていた(と私のよう がapp/admin/admin_user.rb
を削除(あるいはあなたがUninitialized constant error on AdminUserを取得します)(のみ必要とされるものを使用)app/admin/user.rb
にapp/admin/admin_user.rb
からroutes.rbを コピーコードからdevise_for :admin_users, ActiveAdmin::Devise.config
を削除します同じように)。
すでにデフォルト設定でActiveAdminをインストールしている、とあなたは既存のモデルにUser.is_admin
フィールドを使用してユーザーを認証し、ADMIN_USERテーブルを削除したい場合はここではプロセスです:
ロールバックADMIN_USERの移行(あなたが使用しなかった場合--skip-users
)アクティブな管理をインストール:
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つのファイルを削除します。ルーティングで
、追加、application_controller.rbでラインをdevise_for :admin_users, ActiveAdmin::Devise.config
を削除:active_admin.rbで
def authenticate_admin!
if current_user && current_user.is_admin
# fine
else
redirect_to new_user_session_path
end
end
を:
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
介してログアウトする工夫を設定するには、 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
は正確にあなたがauthenticate_adminを置かない
をお楽しみください!方法?私はアプリケーションコントローラを試しましたが、私は取得します: 未定義のメソッド 'authenticate_admin_user! ' # –
の場合これをconfig/initializers/active_admin.rbファイルに配置します。 – jackyalcine
またはアプリケーションコントローラ内。 – domachine