【问题标题】:Ruby on Rails Chapter 9 ErrorsRuby on Rails 第 9 章错误
【发布时间】:2013-08-13 18:44:03
【问题描述】:

我正在阅读 Ruby on Rails 教程,并且大部分时间都在完成第 9 章。出于某种原因,我不断收到这三个错误,我在 github 上查看了如果我遗漏了什么怎么办,但是我有我应该有的。这是我的错误:

    1) Authentication signin with valid information followed by signout
     Failure/Error: before { click_link "Sign out" }
     NameError:
       undefined local variable or method `sign_out' for #<SessionsController:0x104c0b9d0>
     # ./app/controllers/sessions_controller.rb:19:in `destroy'
     # (eval):2:in `send'
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:44

  2) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
     Failure/Error: page.should have_selector('title', :text => 'Edit user')
       expected css "title" with text "Edit user" to return something
     # ./spec/requests/authentication_pages_spec.rb:65

  3) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in when signing in again should render the default (profile) page
     Failure/Error: click_link "Sign out"
     NameError:
       undefined local variable or method `sign_out' for #<SessionsController:0x104c358c0>
     # ./app/controllers/sessions_controller.rb:19:in `destroy'
     # (eval):2:in `send'
     # (eval):2:in `click_link'
     # ./spec/requests/authentication_pages_spec.rb:70

我的 authentication_pages_spec.rb 文件:

 require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do

    before { visit signin_path }

    it { should have_selector('h1',    :text => 'Sign in') }
    it { should have_selector('h1','title', :text => 'Sign in') }
end


    describe "signin" do

    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('h1', 'title', :text => 'Sign in') }
      it { should have_error_message }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_error_message }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      it { should have_selector('title', :text => user.name) }
      it { should have_link('Profile',  :href => user_path(user)) }
      it { should have_link('Sign out', :href => signout_path) }
      it { should have_link('Settings', :href => edit_user_path(user)) }
      it { should have_link('Users',    :href => users_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 "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

          describe "when signing in again" do
            before do
              click_link "Sign out"
              click_link "Sign in"
              fill_in "Email",    :with => user.email
              fill_in "Password", :with => user.password
              click_button "Sign in"              
            end

            it "should render the default (profile) page" do
              page.should have_selector('title', :text => user.name)
            end
          end
        end
      end

      describe "in the Users controller" do

        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_selector('title', :text => 'Sign in') }
          it { should have_selector('div.alert.alert-notice') }
        end

        describe "submitting to the update action" do
          before { put user_path(user) }
          specify { response.should redirect_to(signin_path) }
        end

        describe "visiting the user index" do
          before { visit users_path }
          it { should have_selector('title', :text => 'Sign in') }
        end
      end

     describe "as wrong user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:wrong_user) { FactoryGirl.create(:user, :email => "wrong@example.com") }
      before { sign_in user }

      describe "visiting Users#edit page" do
        before { visit edit_user_path(wrong_user) }
        it { should_not have_selector('h1', 'title', :text => full_title('Edit user')) }
      end

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

我的 session_controller.rb 文件:

 class SessionsController < ApplicationController

  def new
  end

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

  def destroy
    sign_out
    redirect_to root_path
  end
end

非常感谢任何帮助!提前谢谢!

【问题讨论】:

    标签: ruby-on-rails rspec capybara rspec-rails


    【解决方案1】:

    您的错误是因为在您的 SessionsController 或其继承自的父 ApplicationController 中没有定义 sign_out 方法。

    根据第 8 章的 Rails 教程,他有你 define this method inSessionsHelper,然后是 mixed inApplicationController。我认为将助手混入控制器并不好(助手应该仅限于视图;others agree),但这就是本教程为您准备的方式。

    【讨论】:

    • 哦!我想我一定是没有保存就退出了,而且我的 sessionsHelper 没有保存。谢谢你发现这个错误!我也很欣赏这个超链接
    【解决方案2】:

    您正在SessionsController 实例中调用sign_out,但没有定义这样的方法。检查“退出”视图中的代码并检查您的routes.rb 文件,以确保您选择的任何链接都已正确映射。通常,“退出”映射到destroy 方法。

    【讨论】:

    • 在我的 routes.rb 文件中,我有 match '/signout', :to => 'sessions#destroy', :via => :delete 我一直在关注本教程,我认为这些是定义“退出”的唯一 3 次
    • 你的视图代码怎么样?click_link "Sign out"调用的代码?
    • 你愿意分享rake routes的输出吗?
    • 是的,没关系。 sign_out 由图 8.29 中定义的 Sessions#destroy 调用,正如其他答案所示,建议在帮助文件中定义,如图 8.30 所示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    相关资源
    最近更新 更多