Rails TypeError:wrong argument type nil (expected Regexp) Rails4 教程第 9 章

snailseason · 2014年09月18日 · 最后由 chrisyeh 回复于 2014年10月11日 · 4094 次阅读

hi 大家好,我正在跟进 rail4 的教程 中文版的,卡在了第 9.2.2 章节,我在 stackoverflow 上也问了这个问题,可惜几个小时过去了 没有人回复。请高手帮忙看看,非常感谢! stackover 链接:http://stackoverflow.com/questions/25909464/why-am-i-receiving-typeerror-wrong-argument-type-nil-expected-regexp

github 代码: https://github.com/Snailseason2014/Sample

报错情况:

  1) Authentication authorization as wrong user submitting a GET request to the Users#edit action 
     Failure/Error: specify { expect(response.body).not_to match(full_title('Edit user')) }
     TypeError:
       wrong argument type nil (expected Regexp)
     # ./spec/requests/authentication_pages_spec.rb:59:in `block (5 levels) in <top (required)>'

Finished in 1.56 seconds
64 examples, 1 failure

app/helpers/sessions_helper.rb

module SessionsHelper
    def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
    end
    def signed_in?
        !current_user.nil?
    end
        def current_user=(user)
            @current_user = user
        end
        def current_user
            remember_token = User.encrypt(cookies[:remember_token])
            @currend_user ||= User.find_by(remember_token: remember_token)
        end
        def current_user?(user)
            user == current_user
        end
        def sign_out
            self.current_user = nil
            cookies.delete(:remember_token)
end
end

spec/requests/authentication_pages_spec.rb

require 'spec_helper'
describe "Authentication" do
    subject { page }
    describe "signin page" do
    before { visit signin_path }
it { should have_content('Sign in') }
it { should have_title('Sign in') }
end
describe "signin" do
    before { visit signin_path }
    describe "with invalid information" do
        before { click_button "Sign in"}

        it { should have_title('Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }

        describe "after visiting another page" do
            before { click_link "Home" }
            it { should_not have_selector('div.alert.alter-error') }
        end
    end
    describe "with valid information" do
        let(:user) { FactoryGirl.create(:user) }
        before { sign_in user }

        it { should have_title(user.name) }
        it { should have_link('Profile', href: user_path(user)) }
        it { should have_link('Settings',    href: edit_user_path(user)) }
        it { should have_link('Sign out', href: signout_path) }
        it { should_not have_link('Sign in', href: signin_path) }

    describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
    end
end
end
    describe "authorization" do
        describe "for non-signed-in users" do
            let(:user) { FactoryGirl.create(:user) }
            describe "in the Users controller" do
                describe "visiting the edit page" do
                     before { visit edit_user_path(user) }
                     it { should have_title('Sign in') }
                    end
                    describe "submitting to the update action" do
                        before { patch user_path(user) }
                        specify { expect(response).to redirect_to(signin_path) }
                    end
                end
            end
            describe "as wrong user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") }
      before { sign_in user, no_capybara: true }

      describe "submitting a GET request to the Users#edit action" do
        before { get edit_user_path(wrong_user) }
        specify { expect(response.body).not_to match(full_title('Edit user')) }
        specify { expect(response).to redirect_to(root_url) }
      end

      describe "submitting a PATCH request to the Users#update action" do
        before { patch user_path(wrong_user) }
        specify { expect(response).to redirect_to(root_url) }
      end
    end
  end
end

user_ontroller.rb

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]
def show
    @user = User.find(params[:id])
end
  def new
    @user = User.new
  end
def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "welcome motherfuckers"
      redirect_to @user
    else
      render 'new'
    end
  end
      def edit
        # @user = User.find(params[:id])
      end
      def update
    # @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
end 
end
  private

    def user_params
      params.require(:user).permit(:name, :email, :password,:password_confirmation)
    end
    # Before filters

    def signed_in_user
    redirect_to signin_url, notice: "Please sign in." unless signed_in?
  end
def correct_user
  @user = User.find(params[:id])
  redirect_to(root_path) unless current_user?(@user)
end
end

解决办法: 升级 gemfile 配置文件 gem "rspec-rails", '~> 2.14.0.rc1' 之后运行升级安装

$ bundle update

$ bundle install

需要 登录 后方可回复, 如果你还没有账号请 注册新账号