2015-05-15 26 views
12

Tôi có ActiveAdmin và Devise làm việc với Người dùng. Tôi muốn sử dụng Devise để đăng nhập người dùng không phải quản trị viên thông thường bằng cùng một mô hình Người dùng. Tôi có thể làm cái này như thế nào? (Tôi muốn có một lá cờ admin trong mô hình tài khoản cho quản trị viên mà thôi.) Tôi đã thử thêm dòng thứ 2 để routes.rbLàm cách nào để sử dụng Devise và ActiveAdmin cho cùng một Mô hình người dùng?

devise_for :users, ActiveAdmin::Devise.config 
devise_for :users 

Nhưng nó đã cho một lỗi khi tôi đã cố gắng để liệt kê các tuyến đường

>rake routes 
DL is deprecated, please use Fiddle 
rake aborted! 
ArgumentError: Invalid route name, already in use: 'new_user_session' 
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created 

Tôi đã tạo bộ điều hợp ủy quyền chỉ kiểm tra user.admin == true và đang hoạt động OK cho ActiveAdmin. https://github.com/activeadmin/activeadmin/blob/master/docs/13-authorization-adapter.md

Trả lời

16

Tôi thấy điều này http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin-and-devise/

Nhưng tôi đã kết thúc làm

lập mưu 3.4.1
ActiveAdmin 1.0.0.pre1
Rails này 4.2.1

routes.rb
devise_for :admin_users, {class_name: 'User'}.merge(ActiveAdmin::Devise.config) 
    ActiveAdmin.routes(self) 

    devise_for :users 
    resources :users 
application_controller.rb
def access_denied(exception) 
    redirect_to root_path, alert: exception.message 
    end 
config/initializers/active_admin.rb
config.authorization_adapter = ActiveAdminAdapter 
config.on_unauthorized_access = :access_denied 

(Và thay đổi tất cả các phương pháp _user-admin_user.)

app/models/active_admin_adapter.rb
class ActiveAdminAdapter < ActiveAdmin::AuthorizationAdapter 
    def authorized?(action, subject = nil) 
    user.admin == true 
    end 
end 

rails generate migration add_admin_to_users admin:boolean 
+1

Câu trả lời tuyệt vời .. Thực sự rất hữu ích. – SSR

Các vấn đề liên quan