我是 Ruby on Rails 的新手,我正在学习 Michael Hartl 的教程。我从第 8 章开始,无法弄清楚我错过了哪一步导致失败。任何帮助是极大的赞赏。
Failures:
1) User pages signup with invalid information should not create a user
Failure/Error: expect { click_button submit }.not_to change(User, :count)
ActionView::Template::Error:
undefined method `errors' for nil:NilClass
# ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
# ./app/controllers/users_controller.rb:23:in `create'
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:30:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:30:in `block (4 levels) in <top (required)>'
2) User pages signup with invalid information after submission
Failure/Error: before { click_button submit }
ActionView::Template::Error:
undefined method `errors' for nil:NilClass
# ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
# ./app/controllers/users_controller.rb:23:in `create'
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:33:in `block (5 levels) in <top (required)>'
3) User pages signup with invalid information after submission
Failure/Error: before { click_button submit }
ActionView::Template::Error:
undefined method `errors' for nil:NilClass
# ./app/views/shared/_error_messages.html.erb:7:in `_app_views_shared__error_messages_html_erb___725910499_85961520'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___744286408_86222100'
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___744286408_86222100'
# ./app/controllers/users_controller.rb:23:in `create'
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:33:in `block (5 levels) in <top (required)>'
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@title = "Sign up"
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
end
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
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 }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_selector('title', text: 'Sign up') }
it { should have_content('error') }
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 submit}.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by_email('user@example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
end
app/views/shared/_error_messages.html.erb
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error")%>
</div>
<ul>
<% @users.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>