Skip to content

Instantly share code, notes, and snippets.

@Tasha25
Created November 14, 2013 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tasha25/7458845 to your computer and use it in GitHub Desktop.
Save Tasha25/7458845 to your computer and use it in GitHub Desktop.
Fully explain session
## This is saying the same thing
def current_user
if session[:user_id]
@current_user ||= User.find(session[:user_id])
end
end
helper_method :current_user
end
Learning:
You are using a method called current_user. If there is a user_id in the session hash and current_user is nil then with the user id we have in the session and we know we have a :user_id then go find the user from the database and assign that user and all its properties to @current_user.
This then allows us to do case.user_id or current_user.id or case.name
class ApplicationController < ActionController::Base
protect_from_forgery
private
def current_user
if session[:user_id]
if @current_user.nil?
@current_user = User.find(session[:user_id])
end
end
return @current_user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment