質問

I have finished the first section of chapter 8, only to be greeted with these 2 errors:

Failures:

  1) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `find_by' for #<Class:0xab805ec>
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:18:in `block (4 levels) in <top  required)>'

  2) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `find_by' for #<Class:0xab805ec>
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:18:in `block (4 levels) in <top (required)>'

The required files:

sessions_controller

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      # Sign the user in and redirect to the user's show page.
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

authentication pages spec

require 'spec_helper'

describe "Authentication" do

 subject { page }

 describe "signin page" do
   before { visit signin_path }

   it { should have_content('Sign in') }
   it { should have_title('Sign in') }
 end

 describe "signin" do
   before { visit signin_path }

   describe "with invalid information" do
     before { click_button "Sign in" }

     it { should have_title('Sign in') }
     it { should have_selector('div.alert.alert-error', text: 'Invalid') }
   end

     describe "after visiting another page" do
       before { click_link "Home" }
       it { should_not have_selector('div.alert.alert-error') }
     end
   end

   describe "with valid information" do
     let(:user) { FactoryGirl.create(:user) }
     before do
       fill_in "Email",    with: user.email.upcase
       fill_in "Password", with: user.password
       click_button "Sign in"
    end

    it { should have_title(user.name) }
    it { should have_link('Profile',     href: user_path(user)) }
    it { should have_link('Sign out',    href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
    end

end 

I started researching by putting in various snippets of the error and read through similar questions and tried their solutions but to no avail. Even staring at the code, I figured out the problem is to do with the create method inside sessions controller and specifically find_by method. Being fairly new to rails and to its concepts I am still puzzled. Any help and an explanation would be greatly appreciated.

役に立ちましたか?

解決

I think you are using older version of rails

For Rails 4

User.find_by(email: params[:session][:email].downcase)

For older version try

User.find_by_email(params[:session][:email].downcase) 

他のヒント

This should do:

user = User.find_by_email(params[:session][:email].downcase)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top