Rails 3 - Can Active_admin use an existing user model?

Can Active Admin use my current Devise model? It already has a column called admin , and if it is true , I would bypass the Active admin login when switching to /admin .

Is it possible?

Current routes:

 #Active admin ActiveAdmin.routes(self) #Devise devise_for :admin_users, ActiveAdmin::Devise.config devise_for :users, :path => "account" 

The rest is mostly standard Devise + Active admin

+45
ruby-on-rails-3 devise activeadmin
Dec 02 '11 at 18:18
source share
4 answers

Yes, you can do this when the generator starts , skip creating a user model:

 rails generate active_admin:install --skip-users 

Then in 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! 

uncomment config.authentication_method and specify your authentication method for your administrator, for example:

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

Reboot the server and it should work. Also see Active Admin Configuration

Hope this helps.

+62
Dec 02 '11 at 18:31
source share

As mentioned earlier, you will need to update your config/initializers/active_admin.rb to display the correct auth method.

In addition, you will also want to update the following settings:

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

to

 config.current_user_method = :current_user 

and

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

to

 config.logout_link_path = :destroy_user_session_path 

Of course, you don't need to update them (or the method mentioned in the post), and just rewind the methods elsewhere, but this is apparently the easiest / cleanest approach. You will obviously need to replace "user" in each setting ( current_USER ) with the model name using special authentication.

I would also recommend updating the following setting while you are there:

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

to

 config.logout_link_method = :delete 

This last change is required if the default HTTP method used by your device’s configuration is set to :delete , which it is, if you did not change it. It’s important that they are now synchronized, because if you follow these instructions, you will use destroy_user_session_path , which is the path already defined during development. Otherwise, you will receive a message stating that the route [GET] / users / sign_out does not exist.

+24
Feb 01 '13 at 17:23
source share

Everything that everyone else said, as well as in connection with the guidance set out at http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin- and-devise /

which adds some extra bits to the information if you decide to return to the option to have one user model when you have already implemented the admin_user model (that is, right now you have a “user” and also “admin_user”).

Additional steps included

remove devise_for :admin_users, ActiveAdmin::Devise.config from devise_for :admin_users, ActiveAdmin::Devise.config routes copy the code from app/admin/admin_user.rb to app/admin/user.rb (use only what is required) delete app/admin/admin_user.rb ( or you will get an uninitialized persistent error in AdminUser ), like this guy (and me too).

+4
Mar 27 '14 at 8:08
source share

Here is the process if you have already installed ActiveAdmin with default settings and want to authenticate users with the User.is_admin field in your existing model and delete the admin_user table:

Debug rollback admin_user (if you did not use --skip-users when installing Active Admin):

 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 

Then delete these 3 files.

In routing, delete the line devise_for :admin_users, ActiveAdmin::Devise.config

In application_controller.rb add:

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

In 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. 

To configure the device to exit via :get , add to devise.rb:

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

Create is_admin migration:

 rails g migration add_is_admin_to_user is_admin:boolean 

Edit the migration as follows:

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

And migrate:

 rake db:migrate 

If on rails 4, remember to add is_admin to allow_params. In the app /admin/user.rb:

 permit_params ....., :is_admin 

Add administrator rights to users in the console:

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

Enjoy

+3
Dec 08 '14 at 15:02
source share



All Articles