RailsTutorial 10.1 giving persistent "undefined local variable or method 'authenticate'" errors
I am very new to programming, and so far I've had no big issues with Rails Tutorial.
But when I hit 10.1, I started getting the same errors over and over again and I'm not sure why. I have looked up similar issues at the RailsTutorial Get Satisfaction group here.
Thanks in advance for your help.
Failures:
1) UsersController GET 'edit should be successful
Failure/Error: get :edit, :id => @other_user
undefined local variable or method `authenticate' for #<UsersController:0x270dc60>
# ./spec/controllers/users_controller_spec.rb:115:in `block (3 levels) in <top (required)>'
2) UsersController GET 'edit should have right title
Failure/Error: get :edit, :id => @other_user
undefined local variable or method `authenticate' for #<UsersController:0x1ff7aac>
# ./spec/controllers/users_controller_spec.rb:120:in `block (3 levels) in <top (required)>'
Here's the spec documentation:
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
before(:each) do
@user = Factory(:user)
end
it "should be successful" do
get :show, :id => @user.id
response.should be_success
end
it "should find the right user" do
get :show, :id => @user
assigns(:user).should == @user
end
it "should have the right title, the user's name" do
get :show, :id => @user
response.should have_selector('title', :content => @user.name)
end
it "should have the user's name" do #in the body of the profile
get :show, :id => @user
response.should have_selector('h1', :content => @user.name)
end
it "should have a profile image" do
get :show, :id => @user
response.should have_selector('h1>img', :class => "gravatar")
end
it "should have the right URL" do
get :show, :id => @user
response.should have_selector('td>a', :content => user_path(@user),
:href => user_path(@user))
开发者_如何转开发 end
end
describe "GET 'new'" do
it "should be successful" do
get :new
response.should be_success
end
it "should have the right title" do
get :new
response.should have_selector("title", :content => "Sign up")
end
end
describe "POST 'create'" do
before(:each) do
@attr = {:name => "", :email => "", :password => "",
:password_confirmation => ""}
end
it "should not create a user" do
lambda do
post :create, :user => @attr
end.should_not change(User, :count)
end
it "should have the right title" do
post :create, :user => @attr
response.should have_selector('title', :content=> "Sign up")
end
it "should render the 'new' page" do
post :create, :user => @attr
response.should render_template('new')
end
describe "success" do
before(:each) do
@attr={ :name => "A Chowdhury", :email => "achowdhury@gmail.com",
:password => "cqsoqaqa", :password_confirmation => "cqsoqaqa"}
end
it "should create a user" do
lambda do
post :create, :user => @attr
end.should change(User, :count).by(1)
end
it "should sign the user in" do
post :create, :user => @attr
controller.should be_signed_in
end
it "should redirect to the user 'show' page" do
post :create, :user => @attr
response.should redirect_to(user_path(assigns(:user)))
end
it "should have a welcome message" do
post :create, :user => @attr
flash[:success].should =~ /welcome to the sample app/i
end
end
end
describe "GET 'edit" do
before(:each) do
@other_user = Factory(:user)
test_sign_in(@other_user)
end
it "should be successful" do
get :edit, :id => @other_user
response.should be_success
end
it "should have right title" do
get :edit, :id => @other_user
response.should have_selector('title', :content => "Edit user")
end
end
end
And the Users Controller:
class UsersController < ApplicationController
before_filter :authenticate, :only => [:edit, :update]
def show
@user = User.find(params[:id])
@title = @user.name
end
def new
@user= User.new
@title= "Sign up"
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
redirect_to @user
flash[:success] = "Welcome to the Sample App!"
else
@title = "Sign up"
render 'new'
end
end
def edit
@user = "Edit user"
end
end
Factory.define :user do |user|
user.name "Joynul Choudhury"
user.email "jcny816@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
UPDATE: authenticate method in User.rb
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.]+@[a-zA-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
# Return true if the user's password matches the submitted password.
def has_password?(submitted_password)
# Compare encrypted_password with the encrypted version of
# 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 User.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
return nil if user.nil?
return user if user.salt == cookie_salt
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
It's looking for an authenticate
method in your UsersController
and not finding one. It is defined in Listing 10.11 in Section 10.2.
I was getting an identical error. After 30 minutes I realized I had miss-named the file:
app/views/users/edit.html.erb
as
app/views/users/edit.html.rb
Renamed the file with the expected extension. Saved. rspec passed.
Hope that helps -
Perry
精彩评论