【问题标题】:Admin Management User devise routing ruby on rails example管理员管理用户在 Rails 示例上设计路由 ruby
【发布时间】:2016-03-14 14:04:48
【问题描述】:

假设我有 3 个设计模型(管理员、管理、用户),并且我有文章模型

我想用这种方式创建url(路由):

  • 用于设计模型

    本地主机/管理员

    本地主机/管理

    本地主机/用户

  • 针对特定设计用户的文章

    localhost/admin/articles(管理员的文章)

    localhost/management/articles(管理的文章)

    localhost/user/articles(用户的文章)

有人可以创建一个简单的示例或指导我如何在 ruby​​ on rails 上配置此类设置吗?

我用初始代码创建了一个 git。你能把这个问题贡献到这个 git 示例中吗? https://github.com/axilaris/admin_user_devise_articles

【问题讨论】:

    标签: ruby-on-rails url devise model routes


    【解决方案1】:

    您可以使用命名空间。在http://guides.rubyonrails.org/routing.htmlhttp://guides.rubyonrails.org/routing.html的 Rails 路由指南中有一个很好的描述

    您还可以通过添加authenticate 来强制在上述命名空间中进行身份验证,如下所示:

    namespace :admin do
      authenticate :admin do
        resources :articles, only: :index
      end
    end
    

    最后,您可以在登录和/或注销路径后指定设计,将以下内容添加到app/controllers/application_controller.rb

    def after_sign_in_path_for(resource)
      if resource.is_a?(User)
        user_articles_path # or whatever path you want
      elsif resource.is_a?(Admin)
        ...
      elsif resource.is_a?(Management)
        ...
      end
    end
    
    def after_sign_out_path_for(resource)
      new_session_path(resource) # or whatever path you want
    end
    

    【讨论】:

    • 仍然不确定它是如何完成的,我已经在github.com/axilaris/admin_user_devise_articles 中添加了这个分支 test_namespace_branch。 “未初始化的常量 Admin::ArticlesController”当我进入 localhost:3000/admin/articles
    • 您需要在 admin 命名空间中添加一些文章控制器。我会快速看看你的 git...
    • 我有一些工作。结帐大师。我在管理员和用户下创建了articles_controller.rb。 github.com/axilaris/admin_user_devise_articles/blob/master/app/…。现在与 localhost:3000/admin/articles 一起工作。现在正在尝试使身份验证正常工作。
    • 我克隆了你的仓库。是的,这对我来说是正确的。干得好。
    • 谢谢。但是我无法让设计正常工作。 localhost:3000/admins/sign_in 抱怨“无法重定向到零!”。另外,我如何保护 localhost:3000/admin/articles 仅使用“admin”模型身份验证而不是用户身份验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2012-03-23
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多