开发者

How to achieve a separated Admin (namespaced or scoped) with two devise models (User & Admin) in Rails 3?

开发者 https://www.devze.com 2023-03-05 18:49 出处:网络
Consider this as a challenge rather than its general approach.The reason I mention this is because, it is generally preferred to incorporate admin-accessible features into the public facing site.This

Consider this as a challenge rather than its general approach. The reason I mention this is because, it is generally preferred to incorporate admin-accessible features into the public facing site. This is what's required:

  • Devise model for Users, visitors accessing the public facing site
  • Devise model for Admins
  • Namespace or scope the admin 'area' to /admin. Admins can only login from this route.
  • Users can sign up directly from the site's public facing landing page; they are not forced to visit /users/sign_up as per the default devise generated route.
  • Consider overriding the default devise controllers

Thank开发者_运维知识库s, Mike.


The following seems like I've made some progress in the right direction; this at least provides identical out-of-the-box devise functionality for both users and admins, with the custom routing,

  match 'admin', :controller => 'admin'

  namespace :admin do
    # to be updated later... 
  end

  devise_for :users
  #devise_for :admins, :path => "admin"  # this works but uses the default
                                         # Devise::SessionController
  devise_for :admins, 
    :controllers => { 
      :sessions => "admin/sessions", 
      :passwords => "admin/passwords",
      :registrations => "admin/registrations" }, :path => "admin",
    :skip => [:sessions, :passwords, :registrations] do
    get 'admin/sign_in' => 'admin/sessions#new', :as => :new_admin_session
    post 'admin/sign_in' => 'admin/sessions#create', :as => :admin_session
    get 'admin/sign_out' => 'admin/sessions#destroy', :as => :destroy_admin_session

    get 'admin/sign_up' => 'admin/registrations#new', :as => :new_admin_registration
    get 'admin/account' => 'admin/registrations#edit', :as => :edit_admin_registration
    post 'admin/account' => 'admin/registrations#create', :as => :admin_registration
    get 'admin/cancel' => 'admin/registrations#cancel', :as => :cancel_admin_registration
    put 'admin/account' => 'admin/registrations#update'
    delete 'admin/account' => 'admin/registrations#destroy'

    post 'admin/password' => 'admin/passwords#create', :as => :admin_password
    get 'admin/password/new' => 'admin/passwords#new', :as => :new_admin_password
    get 'admin/password/edit' => 'admin/passwords#edit', :as => :edit_admin_password    
    put 'admin/password' => 'admin/passwords#update'
  end

Ideas?

caveat: in this example, I've included the :registerable devise module in the Admin model just for testing during development. The sign_up route will, ultimately, be removed.

Much searching yielded (mind the pun) the following blog post that seems to indicate overriding a devise controller requires the re-mapping of all its specified 'HTTP verbs' as it were; this makes sense as unmapped ones would be handled by the default devise controller.

If anyone has more experience working with multiple devise models and the separated admin approach, I would be very much interested in your thoughts and suggestions!

0

精彩评论

暂无评论...
验证码 换一张
取 消