Michael Hartl Tutorial v. 3.2 Capítulo 7.17 NomethodError en usuarios # Nueva línea 4

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

  •  14-11-2019
  •  | 
  •  

Pregunta

Después de buscar en este foro, he encontrado que estoy parado en este tema.Estoy ejecutando rieles 3.2.1 y Ruby 1.9.3

He seguido el libro de Hartl muy de cerca y estoy experimentando un error en las pruebas y al hacer la página de registro.

Aquí está el error con alguna traza:

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'

aquí es mi nuevo.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 %>

y aquí está mi usuario_controller.rb

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

  def new
    @user = User.new
  end

  def new
  end
end

La prueba que está fallando:

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

y los mensajes de error 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

Cualquier ayuda con esto sería muy apreciada.Gracias de antemano, Brian.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top