Siva Siva - 6 days ago 10
Ruby Question

Ruby on rails - Helper method - undefined method `log_in' in Ruby on rails

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

Answer

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