如何在 Rails 3 中使用两个设计模型(用户和管理员)实现独立的管理员(命名空间或范围)?

发布于 2024-11-07 02:11:38 字数 407 浏览 0 评论 0原文

将此视为一个挑战,而不是其一般方法。我提到这一点的原因是,通常首选将管理员可访问的功能合并到公共面向站点。这就是所需要的:

  • 为用户、访问面向公众的网站的访问者
  • 设计模型为管理员
  • 命名空间设计模型或将管理“区域”范围限定为/admin。管理员只能从此路径登录。
  • 用户可以直接从网站面向公众的登陆页面进行注册;他们不会被迫按照默认设计生成的路线访问 /users/sign_up
  • 考虑覆盖默认的设备控制器

谢谢, 麦克风。

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

Thanks,
Mike.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-11-14 02:11:38

以下似乎我已经在正确的方向上取得了一些进展;这至少为用户和管理员提供了相同的开箱即用的设计功能,以及自定义路由,

  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

想法?

警告:在此示例中,我在管理模型中包含了 :registerable devise 模块,只是用于开发期间的测试。 sign_up 路由最终将被删除。

经过大量搜索(注意双关语),以下博客文章似乎要指示覆盖设备控制器,需要重新映射其所有指定的“HTTP 动词”;这是有道理的,因为未映射的将由默认设备控制器处理。

如果有人有更多使用多个设计模型和独立管理方法的经验,我将对您的想法和建议非常感兴趣!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文