2011年4月23日(星期六)更新
在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我正在继续调查..。哦,joy!
编辑:完成第九课之后,我会在这里发帖--似乎我又回到了我自己找到问题的道路上,这要感谢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_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
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原来的问题如下:
我目前正在学习Rails教程的第9课,在运行自动测试时会遇到以下错误:
失败:
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
endsessions_helper.rb:
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end在GrahamJRoy中添加application_controller.rb中的“include SessionsHelper”
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end两个新的错误,我认为是导致我的问题:
失败:
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)>'发布于 2011-04-20 20:28:50
根据文档,您在ApplicationController中引用了ApplicationController吗?
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end发布于 2011-04-23 11:26:44
因此,我基本上回过头来,重新开始上课,并仔细地注释掉了每一页上不应该出现的代码。
上面的星期六更新让我开始检查故障显示了什么,所以我查看了其中提到的每一行,并检查了RailsTutorial git,以查看任何明显的差异。
这导致了*旁边的remember_token
User.authenticate_with_salt(*remember_token)当添加*错误时,
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至:
Failures:
1) SessionsController POST 'create' success should sign the user in
Failure/Error: controller.should be_signed_in
NoMethodError:
undefined method `signed_in?' for #<SessionsController:0x00000104086088>
# ./spec/controllers/sessions_controller_spec.rb:59:in `block (4 levels) in <top (required)>'
Finished in 1.73 seconds
63 examples, 1 failure这时,我检查了我的代码:
def signed_in
!current_user.nil?
end对吉特人说:
def signed_in?
!current_user.nil?
end很方便地看到了“?”我想我会加进去看看会发生什么..。
显然我现在是绿色的..。让我们现在就完成这节课!
非常感谢GrahamJRoy的投入!
https://stackoverflow.com/questions/5736094
复制相似问题