Question

I'm using Clearance for authentication and am attempting to set the root depending on whether the user is logged in or not. Logging in, logging out and creating/deleting users works fine.

I'm following this article from Thoughtbot where I've added a constraint to my route.

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

# Visitor
root to: "home#index"

And I've also created signed_in_constraint.rb in /lib/clearance which performs the check:

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

If I open up rails console and check Clearance, I get no errors so it is loading the Clearance module but not SignedInConstraint from 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

I've also restarted my server (running Anvil and PostgresApp) multiple times as per this ticket on the Github repo.

Using Rails 4 on Ruby 2.0.

Thanks

Was it helpful?

Solution

Constraints are built into Clearance now. You can do stuff like the following:

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

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top