Reputation: 14499
After searching on this forum, I have found I'm at a standstill on this issue. I am running rails 3.2.1 and ruby 1.9.3
I have followed the Hartl book very closely and am experiencing an error in testing and in rendering the signup page.
Here is the error with some 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'
Here is 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 %>
And here is 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
The test that is failing:
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: "[email protected]"
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
And the error messages from 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
Any help with this would be much appreciated. Thanks in advance, Brian.
Upvotes: 1
Views: 1487
Reputation: 1719
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.
Upvotes: 3