0

我在 mac osx10.8 上使用 ruby​​ 1.8.7。我不知道这些错误是什么意思。我目前正在阅读 ruby​​ on rails 教程的第 9 章。任何帮助是极大的赞赏!

1) Authentication signin with invalid information
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:21

  2) Authentication signin with invalid information
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:21

  3) Authentication signin with invalid information after visiting another page
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:21

  4) Authentication signin with valid information
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  5) Authentication signin with valid information
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  6) Authentication signin with valid information
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  7) Authentication signin with valid information
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  8) Authentication signin with valid information
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  9) Authentication signin with valid information
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  10) Authentication signin with valid information followed by signout
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:34

  11) 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: click_button "Sign in"
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:60

  12) 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_button "Sign in"
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:60

  13) Authentication authorization for non-signed-in users as wrong user visiting Users#edit page
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:106

  14) Authentication authorization for non-signed-in users as wrong user submitting a PUT request to the Users#update action
     Failure/Error: before { sign_in user }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/authentication_pages_spec.rb:106

  15) User pages index
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12

  16) User pages index
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12

  17) User pages index pagination
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12

  18) User pages index pagination should list each user
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12

  19) User pages signup page with valid information edit page
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  20) User pages signup page with valid information edit page
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  21) User pages signup page with valid information edit page
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  22) User pages signup page with valid information edit with invalid information
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  23) User pages signup page with valid information edit with valid information
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  24) User pages signup page with valid information edit with valid information
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  25) User pages signup page with valid information edit with valid information
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  26) User pages signup page with valid information edit with valid information
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

  27) User pages signup page with valid information edit with valid information
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:8:in `create'
     # (eval):2:in `send'
     # (eval):2:in `click_button'
     # ./spec/support/utilities.rb:13:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:78

我的会话控制器:

class SessionsController < ApplicationController

  def new
    @title = "Sign in"
  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

我的 User_pages_spec

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do

    let(:user) { FactoryGirl.create(:user) }

    before(:each) do
      sign_in user
      visit users_path
    end

    it { should have_selector('title', :text => 'All users') }
    it { should have_selector('h1',    :text => 'All users') }

    describe "pagination" do

      before(:all) { 30.times { FactoryGirl.create(:user) } }
      after(:all)  { User.delete_all }

      it { should have_selector('div.pagination') }

      it "should list each user" do
        User.paginate(:page => 1).each do |user|
          page.should have_selector('li', :text => user.name)
        end
      end
    end
  end

  describe "signup page" do
    before { visit signup_path }

    let(:submit) { "Create my account" }

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

  describe "with invalid information" do
    it "should not create a user" do
    expect { click_button submit }.not_to change(User, :count)
  end

  describe "after submission" do
    before { click_button submit }

    it { should have_selector('title', :text => 'Sign up') }
    it { should have_content('error') }
  end
end

  describe "with valid information" do
      before do
        fill_in "Name",         :with => "Example User"
        fill_in "Email",        :with => "user@example.com"
        fill_in "Password",     :with => "foobar"
        fill_in "Confirmation", :with => "foobar"
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
    end

    describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by_email('user@example.com') }

        it { should have_selector('title', :text => user.name) }
        it { should have_selector('div.alert.alert-success', :text => 'Welcome') }
      end

    describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before do
      sign_in user
      visit edit_user_path(user)
    end

    describe "page" do
      it { should have_selector('h1',    :text => "Update your profile") }
      it { should have_selector('title', :text => "Edit user") }
      it { should have_link('change', :href => 'http://gravatar.com/emails') }
    end

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content('error') }
    end

  describe "with valid information" do
      let(:new_name)  { "New Name" }
      let(:new_email) { "new@example.com" }
      before do
        fill_in "Name",             :with => new_name
        fill_in "Email",            :with => new_email
        fill_in "Password",         :with => user.password
        fill_in "Confirm Password", :with => user.password
        click_button "Save changes"
      end

      it { should have_selector('title', :text => new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', :href => signout_path) }
      specify { user.reload.name.should  == new_name }
      specify { user.reload.email.should == new_email }
         end
        end
      end
    end
  end

我的 authentication_pages_spec:

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

新的,html.erb:

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_tag sessions_path do %>

      <%= label_tag :email %>
      <%= text_field_tag :email, params[:email] %>

      <%= label_tag :password %>
      <%= password_field_tag :password %>

      <%= submit_tag "Sign in", :class => "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>
4

1 回答 1

3

错误是由于params[:session]beingnil而导致的,这又是由于您使用form_tag而不是form_for在您的视图中造成的,可能是由于在第 9 章末尾进行了第一个练习。您可以使用,但您必须在in 中form_tag进行相应的更改SessionsController与 RailsCast #270 保持一致(即params[:email]在您的find_by_email电话中引用电子邮件地址。)。

于 2013-08-15T16:23:26.667 回答