Jump to content

The ultimate community for Ruby on Rails developers.


Photo

working the tutorial, question on where @current_user is defined

tutorial

  • Please log in to reply
3 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 18 October 2013 - 06:08 PM

Hi, very new to rails, working through the tutorial by Micheal Hartl,  Im looking at module SessionsHelper after coding it per instructions,  (see code below).  in method def current_user=(user) I see @current_user getting assigned.  However I have no idea where in the world its declaration is???  Can someone guide me here?  thanks in advance...

 

module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end
 
  def current_user=(user)
    @current_user = user  #This sets the instance variable.
  end
 
  def current_user  
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end
 
  def current_user?(user)
    user == current_user
  end
 
  def signed_in?
    !current_user.nil?
  end
 
  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
 
  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

  def store_location
    session[:return_to] = request.url if request.get?
    logger.debug("\n Jay " + session[:return_to])
  end




end
 



#2 rayray

rayray

    Signalman

  • Members
  • 15 posts

Posted 20 October 2013 - 10:21 AM

Do you mean a type declaration?

 

You don't need a declaration in Ruby. It's a dynamically typed language, so the interpreter looks at the type of value you are assigning to the variable and dynamically works out the variable type.


I'm Jack's complete lack of ruby knowledge.


#3 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 21 October 2013 - 01:32 PM

Thanks!!  I was wondering if I should learn Ruby first or go through the rails tutorial first, Micheal does have a short section on Ruby, but I didn't remember any discussion on dynamic attributes..  OK, that makes sense, I thought I tested it with a short pgm at the console, but must have had a different error...   I just now tested it again and you're right.  I also noticed that current_user is called each time a new page is rendered and assume that is something that rails does in parent methods..  Micheal indicates it is called, so it must be part of ActionController..



#4 rayray

rayray

    Signalman

  • Members
  • 15 posts

Posted 21 October 2013 - 02:37 PM

Michael includes the SessionsHelper in the ApplicationController. That's why current_user (and all other methods in the SessionsHelper) are available for all controllers.

 

Yes, learning Ruby is a must, if you really want to work with Rails.

 

If you are new to programming I recommend "Learn to program" (with Ruby) by Chris Pine.

If not new, than "Beginning Ruby" by Peter Cooper or "The Well Grounded Rubyist" by David A. Black


I'm Jack's complete lack of ruby knowledge.






Also tagged with one or more of these keywords: tutorial

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users