我对编程非常陌生,到目前为止,我对Rails Tutorial没有太大的问题。
但是当我点击10.1时,我开始一遍又一遍地得到同样的错误,我不知道为什么。我在RailsTutorial Get Satisfaction小组here中查找了类似的问题。
提前感谢您的帮助。
故障:
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)>'
以下是规范文档:
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
用户控制器:
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
更新:验证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
答案 0 :(得分:3)
它正在authenticate
中寻找UsersController
方法而未找到方法。它在Listing 10.11 in Section 10.2中定义。
答案 1 :(得分:1)
我收到了同样的错误。 30分钟后,我意识到我错过了命名文件:
app/views/users/edit.html.erb
作为
app/views/users/edit.html.rb
使用预期的扩展名重命名该文件。保存。 rspec通过了。
希望有所帮助 -
佩里