使用Clearance的“未初始化的常量”路由错误

时间:2013-10-15 13:09:00

标签: ruby-on-rails ruby authentication ruby-on-rails-4 clearance

我正在使用Clearance进行身份验证,并尝试根据用户是否登录来设置root。登录,注销和创建/删除用户工作正常。

我正在关注这个article from Thoughtbot,我在路线上添加了约束。

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

# Visitor
root to: "home#index"

我还在/ lib / clearance中创建了signed_in_constraint.rb来执行检查:

module Clearance
  class SignedInConstraint
    def self.matches?(request)
      request.env[:clearance].signed_in?
    end
  end
end

如果我打开rails控制台并检查Clearance,我没有错误,所以它正在加载Clearance模块而不是来自lib的SignedInConstraint。

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。

由于

1 个答案:

答案 0 :(得分:0)

现在已将限制内置于清除中。您可以执行以下操作:

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
祝你好运!