1

I am implementing login based on Michael Hartl's Ruby on Rails Tutorial 3rd Edition. Stuck in chapter 8.

Defining a new log_in helper function in SessionsHelper as below. When I try to access the same function in SessionController, I am getting the undefined method log_in:

module SessionsHelper

  def log_in(user)
    session[:user_id] = user.id
  end
end

and SessionCOntroller code is

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
    else
      flash[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end
IainDunning
  • 11,546
  • 28
  • 43
Siva
  • 13
  • 3

1 Answers1

4

You are missing the ApplicationController part here which includes the helper in the controller so its methods can be accessible directly:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end
rafb3
  • 1,694
  • 12
  • 12
  • cool, glad to be helpful. Care to set my answer as the correct one then so this gets off the "unanswered" bucket? – rafb3 Dec 05 '14 at 19:38