我使用Clearance进行身份验证,并试图根据用户是否登录来设置根目录登录、注销和创建/删除用户工作正常。
我正在跟踪这条article from Thoughtbot路线,在这里我给我的路线添加了一个约束。

# Logged in user
root to: "dashboard#index", constraints: Clearance::SignedInConstraint`

# Visitor
root to: "home#index"

我还创建了signed_in_constraint.rb in/lib/clearance来执行检查:
module Clearance
  class SignedInConstraint
    def self.matches?(request)
      request.env[:clearance].signed_in?
    end
  end
end

如果我打开rails控制台并检查Clearance,我不会得到任何错误,因此它正在加载Clearance模块,但没有从lib中签名。
rails console
Loading development environment (Rails 4.0.0)
irb(main):001:0> Clearance
=> Clearance
irb(main):002:0> Clearance::SignedInConstraint
NameError: uninitialized constant Clearance::SignedInConstraint

我还根据github repo上的this ticket多次重新启动了服务器(运行anvil和postgresapp)。
在Ruby 2.0上使用Rails 4。
谢谢

最佳答案

现在,限制条件已内置到清除中你可以做如下事情:

constraints Clearance::Constraints::SignedIn.new { |user| user.admin? } do
  root to: 'admin'
end

constraints Clearance::Constraints::SignedIn.new do
  root to: 'dashboard'
end

constraints Clearance::Constraints::SignedOut.new do
  root to: 'marketing'
end

祝你好运!

10-07 21:46