2

Ruby on Rails 3.2

My sessions_controller.rb checks for 'signed_in_person' to bypass login if they already have a logged session.

My 'signed_in_person' is defined in the session_helper.rb but I get an error:

Started POST "/sessions" for 50.241.102.227 at 2014-02-25 10:12:33 -0800
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"fFo4EM1MOHNO1Thic6qA/uapGj8pbiNaepKYF5Ge0Tw=", "session"=>{"email"=>"dduck@blah.com", "password"=>"[FILTERED]"}, "com
mit"=>"Sign in"}
Person Load (0.1ms)  SELECT `persons`.* FROM `persons` WHERE `persons`.`email` = 'dduck@blah.com' LIMIT 1
Completed 500 Internal Server Error in 66ms

NoMethodError (undefined method `sign_in_person' for #<SessionsController:0x00000004f7ace8>):
app/controllers/sessions_controller.rb:11:in `create'

The Person is in the database table when checked from console. My sessions_controller:

def create
    if params[:session][:email].include? 'blah.com'
        person = Person.find_by_email(params[:session][:email].downcase)
        if person && person.authenticate(params[:session][:password])
            sign_in_person person
            redirect_back_or person
        else
            flash.now[:error] = 'Invalid email/password combination'
            render 'new'
        end
    else
        ...
    end
end

My sessions_helper:

module SessionsHelper

# For Persons

def sign_in_person(person)
    cookies[:remember_token] = person.remember_token
    self.current_person = person
end

def signed_in_person?
    !current_person.nil?
end
    ...

Any ideas as to why my sessions_helper isn't helping?

DDDD
  • 3,790
  • 5
  • 33
  • 55

3 Answers3

3

Helper methods are available from the views, not from the Controller.

To use them from the Controller, (Taken from here) you can either:

  1. include the helper module in the controller
  2. define the helper as a controller method and mark it as a helper via helper_method :method_name.

In your case, since you'll be using several methods from SessionsHelper I think it would be best include the helper like this:

class SessionsController < ApplicationController
  include SessionsHelper


  ...
end
Community
  • 1
  • 1
raviolicode
  • 2,155
  • 21
  • 22
  • This did not change the error, :( If I add, 'include SessionsHelper' to my persons_controller the error went away. Is that okay? – DDDD Feb 25 '14 at 18:48
  • @DDDD It's really weird, since the error log says you tried to call `sign_in_person` from `SessionsController`... did you used the method on `PersonsController` too? – raviolicode Feb 25 '14 at 19:01
  • No but the PersonsController is what is using the SessionsController – DDDD Feb 25 '14 at 19:42
1

This is generally bad practice, but you could use view_context.sign_in_person from your controller. I believe you can also use the helper method in your controller, something like this:

class SessionsController < ApplicationController
  helper :sessions

  ...
end

Helpers are generally supposed to be for views. Like raviolicode said, you can move it to the controller and use helper_method to make it accessible to both your controller and view.

# in controller
def sign_in_person
  ....
end
helper_method :sign_in_person

If you only want it available from your controller, it's probably best to just make a private method inside of the controller itself, or move it into a concern.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
1

If you want to invoke a helper method from within a controller, you'll need to call it according to whether or not it is a class or instance method on the helper:

# app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
  include SessionsHelper

  def create
    person = Person.find_by_email(params[:session][:email].downcase)
    SessionsHelper.sign_in_person person # Class method invocation
    sign_in_person person # Instance method invocation
  end

end

# app/helpers/sessions_helper.rb
module SessionsHelper

  # Instance method
  def sign_in_person(person)
    cookies[:remember_token] = person.remember_token
    SessionHelper.current_person = person
  end

  # Class method
  def self.sign_in_person(person)
    cookies[:remember_token] = person.remember_token
    self.current_person = person
  end

end
zeantsoi
  • 25,857
  • 7
  • 69
  • 61