0

我按照教程写了一个测试场景:当用户去编辑信息页面但还没有登录时,他们会重定向到登录页面,他们登录后会重定向到编辑页面。但是当我按照教程完成所有操作时,运行测试并失败,因为wrong number of arguments (0 for 1)错误。

这是错误信息:

未登录用户在登录后尝试访问受保护页面时的 AuthenticationPages 授权应呈现所需的受保护页面
失败/错误:click_button“登录”
ArgumentError:参数数量错误(0 表示 1)
# ./app/ helpers/sessions_helper.rb:31:in create' # (eval):2:in block (5 levels) in..redirect_back_or'
# ./app/controllers/sessions_controller.rb:11:in

click_button' # ./spec/requests/authentication_pages_spec.rb:58:in

这是我的规范/请求/authentication_pages_spec.rb

describe "authorization" do

  describe "for non-signed-in users" do
    let(:user) { FactoryGirl.create(:user) }

    describe "when attempting to visit a protected page" do
      before do
        visit edit_user_path(user)
        fill_in "Email", with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      describe "after signing in" do

        it "should render the desired protected page" do
          page.should have_selector('title', text: 'Edit user')
        end
      end
    end    
 .
 .
 .

在 app/helpers/sessions_helper.rb 中,我有两种方法,store_location用于redirect_back_or存储并重定向回用户在登录前访问的 url 地址:

module SessionsHelper

  .
  .
  .
  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete[:return_to]
  end

  def store_location
    session[:return_to] = request.url
  end
end

然后我store_location在 app/controllers/users_controller.rb 中使用了方法:

class UsersController < ApplicationController

  before_filter :signed_in_user, only: [:edit, :update]
  before_filter :correct_user, only: [:edit, :update]
  .
  .
  private

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

redirect_back_or在 app/controllers/sessions_controller.rb 中使用了方法:

class SessionsController < ApplicationController

  def new

  end

  def create
    user = User.find_by_email(params[:email].downcase)
    if user && user.authenticate(params[:password])
      sign_in user
      redirect_back_or user
    else
      flash.now[:error] = "Invalid email/password combination"
      render 'new'
    end
  end

我不知道为什么会发生这个错误,我认为该redirect_back_or方法已经接受了参数user,但它仍然是错误的。任何人都可以帮我解决这个问题吗?非常感谢。

4

1 回答 1

2

session.delete[:return_to]为此在 SessionsHelper 中更改它session.delete(:return_to)

于 2012-09-05T18:38:20.703 回答