Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:11:39+00:00 2026-06-14T09:11:39+00:00

I have looked at similar errors but not only will my test wont pass,

  • 0

I have looked at similar errors but not only will my test wont pass, the script will not sign in a user.

Failures:

Finished in 0.41649 seconds 31 examples, 2 failures

Failed examples:

rspec ./spec/controllers/sessions_controller_spec.rb:48 #
SessionsController GET ‘new’ POST ‘create’ success should sign the
user in rspec ./spec/controllers/sessions_controller_spec.rb:54 #
SessionsController GET ‘new’ POST ‘create’ success should redirect to
the user show page

Done.

Error upon signin: NoMethodError in SessionsController#create

undefined method `authenticate’ for #
Rails.root: /Users/lancevelasco/Development/appsample

Application Trace | Framework Trace | Full Trace
app/controllers/sessions_controller.rb:10:in `create’

Code

user.rb

# == Schema Information
#
# Table name: users
#
#  id                 :integer          not null, primary key
#  name               :string(255)
#  email              :string(255)
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  encrypted_password :string(255)
#  salt               :string(255)
#

class User < ActiveRecord::Base
  attr_accessor   :password
  attr_accessible :email, :name, :password, :password_confirmation

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name,  :presence => true,
                    :length   => { :maximum => 50 }
  validates :email, :presence   => true,
                    :format     => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }    
  validates :password, :presence => true,
                        :confirmation => true,
                        :length => { :within => 6..40 }

  before_save :encrypt_password

  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end


  def User.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil  if user.nil?
    return user if user.has_password?(submitted_password) 
  end

  def authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt ) ? user : nil
  end

  private
  def encrypt_password
    self.salt = make_salt if new_record?
    self.encrypted_password = encrypt(password)
  end

  def encrypt(string)
      secure_hash("#{salt}--#{string}")
      end   

  def make_salt
    secure_hash("#{Time.now.utc}--#{password}")
  end

  def secure_hash(string)
    Digest::SHA2.hexdigest(string)
  end     
end  

sessions_controller.rb

 class SessionsController < ApplicationController

  def new
    @title = "Sign in"
  end

  def create
    user = User.authenticate(params[:session][:email],
                             params[:session][:password])
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      render 'new'
    else
      sign_in user
      redirect_back_or user
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end

sessions_helper.rb

module SessionsHelper

  def sign_in_(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user || user_from_remember_token    
  end

  private

    def user_from_remember_token
      User.authenticate_with_salt()
    end

    def remember_token
      cookies.signed[:remember_token] || [nil,nil]
    end
end

user_controller_spec.rb

require 'spec_helper'

describe SessionsController do

  render_views

    describe "GET 'new'" do
    it "returns http success" do
      get 'new'
      response.should be_success
    end

    it "should have the right title" do
       get :new
       response.should have_selector('title', :content => "Sign in")
     end

     describe "POST 'create'" do

       describe "failure" do

          before(:each) do
            @attr = { :email => "", :password => ""}
          end 

          it "should re-render the new page" do
            post :create,  :session => @attr 
            response.should render_template('new')
          end

          it "should have the right title" do
            post :create, :session => @attr
          end

          it "should have an error message" do
            post :create, :session => @attr
            flash.now[:error].should =~ /invalid/i
          end
       end

       describe "success" do

         before(:each) do
           @user= Factory(:user)
           @attr = { :email => @user.email, :password => @user.password }
         end

         it "should sign the user in" do
           post :create, :session => @attr
           controller.current_user.should == @user
           controller.should  be_signed_in
         end

         it "should redirect to the user show page" do
           post :create, :session => @attr
           response.should redirect_to(user_path(@user))
         end

       end

     end
  end

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:11:41+00:00Added an answer on June 14, 2026 at 9:11 am

    From what I understand, Michael Hartl used to use bcrypt to handle his authentication (has_secure_password“). Looks like he opted to drop this and write his own authentication (looks like in order to add a salt…very good indeed).

    You have in user.rb:

    def User.authenticate(email, submitted_password)
      user = find_by_email(email)
      return nil  if user.nil?
      return user if user.has_password?(submitted_password) 
    end
    

    So as you can see, you need to pass the email to the authenticate method as well, and since it also grabs the user, you can simplify the session#create method. Try this:

    def create
      user = User.authenticate(params[:session][:email], params[:session][:password])
      if user.nil?
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
      else
        sign_in user
        redirect_back_or user
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked everywhere but have not found a problem similar to this. I
I have looked for similar questions, but could find none other than the difference
I have looked but have not found a specific answer for changing just one
This is not a duplicate post. I've looked through similar questions on SO but
I have looked at this and this which both describe similar problems but don't
Have already looked at these similar issues, but have had no joy: PHP MySQL
I've looked through similar questions but not found the exact working solution I'm after.
i know, similar questions have been asked, and i've already looked through everything i
Have looked quite hard for this answer but having no luck. I have 3
I have a problem with Gridview sorting that is similar to others but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.