更新星期六2011年4月23日Ruby on Rails的教程第9课
课是实现后user.rb以下绿色
def authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end
我得到:
Failures:
1) SessionsController POST 'create' success should sign the user in
Failure/Error: controller.current_user.should == @user
ArgumentError:
wrong number of arguments (1 for 2)
# ./app/models/user.rb:45:in `authenticate_with_salt'
# ./app/helpers/sessions_helper.rb:36:in `user_from_remember_token'
# ./app/helpers/sessions_helper.rb:14:in `current_user'
# ./spec/controllers/sessions_controller_spec.rb:58:in `block (4 levels) in <top (required)>'
Finished in 0.54296 seconds
7 examples, 1 failure
我继续调查......哦,欢乐! :d
编辑:在完成第9课,然后将回到这里后工作 - 似乎,我回来到路径上找到自己的问题再次感谢GrahamJRoy!
更新星期五2011年4月22日:
我花了最后一天试图找到类似的问题,但一直没有引起人们不成功的,如果您需要任何更多信息,请让我知道,无论是失败极有可能彼此相关的,如果我解决一个我认为这将解决其他
Failures:
1) SessionsController POST 'create' success should sign the user in
Failure/Error: post :create, :session => @attr
NameError:
undefined local variable or method `clear_return_to' for #<SessionsController:0x00000101648ac8>
# ./app/helpers/sessions_helper.rb:28:in `redirect_back_or'
# ./app/controllers/sessions_controller.rb:16:in `create'
# ./spec/controllers/sessions_controller_spec.rb:51:in `block (4 levels) in <top (required)>'
2) SessionsController POST 'create' success should redirect to the user show page
Failure/Error: post :create, :session => @attr
NameError:
undefined local variable or method `clear_return_to' for #<SessionsController:0x00000100ea0690>
# ./app/helpers/sessions_helper.rb:28:in `redirect_back_or'
# ./app/controllers/sessions_controller.rb:16:in `create'
# ./spec/controllers/sessions_controller_spec.rb:57:in `block (4 levels) in <top (required)>'
Finished in 0.42858 seconds
7 examples, 2 failures
在会议helper.rb文件:
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
在sessions_contro ller_spec.rb文件:
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
In sessions_controller.rb file:
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_back_or user
end
end
原题如下:
我目前在第9课工作的Ruby on Rails的教程和运行自动测试时,我收到以下错误:
失败:
1) SessionsController POST 'create' success should sign the user in
Failure/Error: post :create, :session => @attr
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x000001017082d8>
# ./app/controllers/sessions_controller.rb:15:in `create'
# ./spec/controllers/sessions_controller_spec.rb:51:in `block (4 levels) in <top (required)>'
2) SessionsController POST 'create' success should redirect to the user show page
Failure/Error: post :create, :session => @attr
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x00000100ecbca0>
# ./app/controllers/sessions_controller.rb:15:in `create'
# ./spec/controllers/sessions_controller_spec.rb:57:in `block (4 levels) in <top (required)>'
sessions_controller_spec.rb:
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
如果我注释掉上述sessions_controller_spec.rb我会变绿!可能这会帮助某人指引我正确的方向,因为我很无聊!
sessions_controller.rb:
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_back_or user
end
end
sessions_helper.rb:
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
每GrahamJRoy增加了 '包括SessionsHelper' 在application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
2个新的错误,我认为导致我的问题:
次失败:
1) SessionsController POST 'create' success should sign the user in
Failure/Error: post :create, :session => @attr
NoMethodError:
undefined method `redirect_back_or' for #<SessionsController:0x0000010405adc0>
# ./app/controllers/sessions_controller.rb:16:in `create'
# ./spec/controllers/sessions_controller_spec.rb:51:in `block (4 levels) in <top (required)>'
2) SessionsController POST 'create' success should redirect to the user show page
Failure/Error: post :create, :session => @attr
NoMethodError:
undefined method `redirect_back_or' for #<SessionsController:0x000001030a3c60>
# ./app/controllers/sessions_controller.rb:16:in `create'
# ./spec/controllers/sessions_controller_spec.rb:57:in `block (4 levels) in <top (required)>'
嗯都能跟得上我想补充说的application_controller.rb文件,并将其杀死自动测试:“SAMPLE_APP:无法运行测试” – rzschau 2011-04-20 21:17:00
应包括SessionsHelper(带S) - 现在2个不同的错误 – rzschau 2011-04-20 21:29:15
遗憾的错字。现在听起来您在sessions_helper中缺少以下方法:def redirect_back_or(默认)请参阅清单10.17 – GrahamJRoy 2011-04-21 12:01:39