Michael Hartlチュートリアルv。3.2第7章7章NOMETHODERROR in Users#新しい行4

StackOverflow https://stackoverflow.com/questions/9509230

  •  14-11-2019
  •  | 
  •  

質問

このフォーラムを検索した後、私はこの問題について停止していることがわかりました。私はRails 3.2.1とRuby 1.9.3を実行しています

私はHARTL Bookを非常に密接に従っており、テストのテストや登録のレンダリングのエラーが発生しました。

これはいくつかのトレースのエラーです:

undefined method `model_name' for NilClass:Class

Extracted source (around line #4):

1: <% provide(:title, 'Sign up') %>
2: <h1>Sign up</h1>
3: 
4: <%= form_for(@user) do |f| %>
5:   <div class="field">
6:     <%= f.label :name %><br />
7:     <%= f.text_field :name %>

Rails.root: /Users/Brian/Sites/rails/brightspot_1-1
Application Trace | Framework Trace | Full Trace

app/views/users/new.html.erb:4:in     
`_app_views_users_new_html_erb___4096651331723577149_70289685515940'
.

これはmy new.html.erbです:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>
.

とここにmy users_controller.rb

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def new
  end
end
.

失敗したテスト:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

  describe "signup" do

      before { visit signup_path }

      describe "with invalid information" do
        it "should not create a user" do
          expect { click_button "Sign up" }.not_to change(User, :count)
        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 "Sign up" }.to change(User, :count).by(1)
        end
     end
   end

end
.

とRSPECからのエラーメッセージ:

Failures:

  1) User pages signup with invalid information should not create a user
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       undefined method `model_name' for NilClass:Class
     # ./app/views/users/new.html.erb:4:in     
`_app_views_users_new_html_erb___947544063866573638_70125101083220'
     # ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

  2) User pages signup with valid information should create a user
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       undefined method `model_name' for NilClass:Class
     # ./app/views/users/new.html.erb:4:in     
`_app_views_users_new_html_erb___947544063866573638_70125101083220'
     # ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

Finished in 0.20414 seconds
2 examples, 2 failures
.

これを使うのはあらゆる助けがしています。前もってありがとう、ブライアン。

役に立ちましたか?

解決

That error occurs when the @user object you're passing to form_for is nil. If you look in your controller, the new method is defined twice, and in the second definition it does not instantiate a @user object.

Delete the second (empty) definition of the new method in your controller and you should be good to go.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top