Skip to content

Instantly share code, notes, and snippets.

@hovancik
Created July 18, 2018 07:25
Show Gist options
  • Save hovancik/d1e8fe96dc6f1423ec069d7c9c725146 to your computer and use it in GitHub Desktop.
Save hovancik/d1e8fe96dc6f1423ec069d7c9c725146 to your computer and use it in GitHub Desktop.
current attribures
class Current < ActiveSupport::CurrentAttributes
  attribute :user
end

application controller:

class ApplicationController < ActionController::Base
  before_action :authenticate

  private

  def authenticate
    if authenticated_user = User.find_by(auth_token: cookies.encrypted[:auth_token])
      Current.user = authenticated_user
      // printing Current.user here shows it correctly #<User:0x00007ff02e2e08d0>
    end
  end
end

view:

<%= Current.user.class %>

=> NilClass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment