【问题标题】:Default Route for an admin User (Devise)管理员用户的默认路由(设计)
【发布时间】:2013-01-03 09:52:00
【问题描述】:

这里的小问题,我似乎无法将管理员用户路由到适当的位置。我已经设置设计并添加了列管理员并将其设置为布尔值。然后我将我的用户更新为 admin => true 并在控制台中验证了这一点。

当我登录到我的应用程序时,用户被路由到一个页面,而管理员用户应该被路由到另一个页面,这是我目前所拥有的

  authenticated :current_admin_user do
  root :to => 'book#searchbook'
  end

  authenticated :user do
   root :to => 'search#index'
  end

  root :to => 'main#index'

但是,当我以管理员用户身份登录时,我会被路由到“search#index”,就好像我是普通用户一样。我需要做什么才能将管理员用户路由到“book#searchbook”。我以前从来没有遇到过这个问题

任何帮助表示赞赏

编辑

好的,所以经过更多研究后,我需要为管理员用户指定 after_sign_in_path,到目前为止我有这个

def after_sign_in_path_for(resource)
  if current_admin_user
searchbook_path
  else
root_path
end
end

但它仍然将我引导到用户登录页面

谢谢

【问题讨论】:

    标签: ruby-on-rails-3 devise routes


    【解决方案1】:

    好吧,我好像有很多遗漏,所以如果其他人在此之前遇到过这样的问题,这就是我所做的

    在我添加的用户模型中

    def is_admin?
     self.admin == 1
    end
    

    然后在我的应用程序控制器中我添加了这些方法

      def authenticate_admin_user!
      authenticate_user!
        unless current_user.admin?
        flash[:alert] = "This area is restricted to administrators only."
        redirect_to root_path
      end
     end
    
    def current_admin_user
      return nil if user_signed_in? && !current_user.admin?
      current_user
    end
    
    def after_sign_in_path_for(resource)
    if current_user.admin?
    searchbook_path
    
    else
     root_path
    end
    end
    

    然后在我添加 before_filter 的管理员用户才能访问的控制器中

    before_filter :authenticate_admin_user!
    

    希望这对处于相同情况的其他人有所帮助(感谢 Jayson Lane)

    原来他在 9 个月前帮助我解决了同样的问题...

    【讨论】:

    • 哈哈@Richlewis,我刚刚用谷歌搜索了这个......偶然发现了这个答案
    【解决方案2】:

    试着实现下面的代码..因为我在我的控制器中使用了这个代码..

    def after_sign_in_path_for(resource_or_scope)
      if resource_or_scope.is_a?(User)
         return root_path
      else
        return search_path
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      相关资源
      最近更新 更多