Tutoriel Michael Hartl v. 3.2 Chapitre 7.17 Nométhoderror chez les utilisateurs # Nouvelle ligne 4

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

  •  14-11-2019
  •  | 
  •  

Question

Après avoir cherché sur ce forum, j'ai trouvé que je suis à l'arrêt sur cette question.Je passe des rails 3.2.1 et Ruby 1.9.3

J'ai suivi le livre en Hartl de très près et je ressens une erreur lors du test et de rendre la page d'inscription.

Voici l'erreur avec une trace:

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'

Voici mon nouveau.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 %>

et voici mes utilisateurs_controller.rb

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

  def new
    @user = User.new
  end

  def new
  end
end

Le test qui échoue:

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

et les messages d'erreur de 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

Toute aide avec cela serait très appréciée.Merci d'avance, Brian.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top