Created
November 14, 2013 00:03
-
-
Save Tasha25/7458845 to your computer and use it in GitHub Desktop.
Fully explain session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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