2012-03-01 35 views
1

このフォーラムで検索したところ、私はこの問題について不安を抱いていました。私はレール3.2.1とルビー1.9.3を実行していますMichael Hartlチュートリアルv.3.2第7.17節ユーザーのNoMethodError#new Line 4

私はHartlの本を非常によく辿り、テストやサインアップページのレンダリングでエラーが発生しています。ここで

は、いくつかのトレースとの誤差である:ここでは

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' 

は私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 %> 

そして、ここでは私のusers_controller.rb

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

    def new 
    @user = User.new 
    end 

    def new 
    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 

これで任意の助けもいただければ幸いです。事前に感謝、ブライアン。

答えて

3

form_forに渡しているオブジェクトが@userの場合、このエラーが発生します。あなたのコントローラを見ると、newメソッドが2回定義され、2番目の定義では@userオブジェクトがインスタンス化されません。

コントローラのnewメソッドの2番目の(空の)定義を削除してください。

+0

これは機能します!ありがとう。 –

+0

もしうまくいくなら、@ wpgreenwayに正しい答えを与えるべきです。 –

関連する問題