Ruby on Rails教程第9课

时间:2011-04-20 20:15:06

标签: ruby-on-rails

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

我正在继续调查......哦,快乐! :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_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

以下原始问题:


我目前正在使用Ruby on 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
  end

sessions_helper.rb:

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end

根据GrahamJRoy添加'在application_controller.rb中包含SessionsHelper'

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)>'

2 个答案:

答案 0 :(得分:5)

根据文档,您是否在ApplicationController中引用了SessionsHelper?

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
end

答案 1 :(得分:0)

所以我基本上回过头来重新开始上课,仔细评论每页不应该出现的代码。

上面的星期六更新让我开始检查故障显示的内容,所以我去了它提到的每一行并检查了RailsTutorial git repo以查看任何明显的差异。

这导致了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

到git repoos:

  def signed_in?
    !current_user.nil?
  end

并且方便地看到了'?'遗失了,我想我会添加,看看会发生什么..

显然我现在是绿色的......现在让我们完成这一课!

非常感谢GrahamJRoy的投入!