title: Rails Tutorial Michael hartl. Chapter 8 sign_in method, Why have we used writer method?
body: <p>In Michael Hartls rails tutorial , I came across the following code :</p>
  
  <pre><code>module SessionsHelper 
  def current_user=(user)
    @current_user = user
   end
  
   def current_user  #get logged in user
    @current_user||=User.find_by_remember_token(:remember_token)
   end
  
   def sign_in(user)   #sign the user in by setting cookies
    cookies.permanent[:remember_token]= user.remember_token
    current_user = user
   end
  
   def signed_in?(user)   #check whether user signed in
    !current_user.nil?
   end
  
  end
  </code></pre>
  
  <p>My SessionsController create action looks like this :</p>
  
  <pre><code>def create
   user = User.find_by_email(params[:session][:email])
   if user &amp;&amp; user.authenticate(params[:session][:password])
    sign_in user   # sign the user in by setting cookies
   else
    flash.now[:error]= "Invalid email/password"
    render 'new'
   end
  end
  </code></pre>
  
  <p>Why do we need a writer method <pre>def current_user=(user)</pre> in the SessionsHelper module ? </p>
  
  <p>Can't we just assign the value of user to the @current_user variable directly in the <pre>sign_in</pre> method ? 
  Can it be done like this:</p>
  
  <pre><code>module SessionsHelper 
  
   # Notice that there is no def current_user=(user) method.
  
   def current_user
    @current_user||=User.find_by_remember_token(:remember_token)
   end
  
   def sign_in(user)
    cookies.permanent[:remember_token]= user.remember_token
    @current_user = user   #set the current user directly withoout a writer method
   end
  
   def signed_in?(user)
    !current_user.nil?
   end
  
  end
  </code></pre>