我正在关注 ruby on rails 教程,我在第 8 课注册失败。我在提交信息时收到模板缺失错误。
Missing template users/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/lexi87/rails_projects/sample_app/app/views"
我仔细检查了他逐行打开的文件。没有什么不同。
users_controllers.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
def new
@user = User.new
@title = "Sign up"
end
def create
@user = User.new(params[:user])
if @user.save
# Handle a successful save.
else
@title = "Sign up"
render = 'new'
end
end
end
新的.html.erb
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages'%>
<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 %>
Error_messages.html.erb
<% if @user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:
</h2>
<p>There were problems with the following fields:</p>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
users_controller_spec.rb
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
before(:each) do
@user = Factory(:user)
end
it "should be successful" do
get :show, :id => @user
response.should be_success
end
it "should find the right user" do
get :show, :id => @user
assigns(:user).should == @user
end
it "should have the right title" do
get :show, :id => @user
response.should have_selector('title', :content => @user.name)
end
it "should have the user's name" do
get :show, :id => @user
response.should have_selector('h1', :content => @user.name)
end
it "should have a profile image" do
get :show, :id => @user
response.should have_selector('h1>img', :class => "gravatar")
end
it "should have the right URL" do
get :show, :id => @user
response.should have_selector('td>a' :content => user_path(@user), :href => user_path(@user))
end
end
describe "GET 'new'" do
it "should be successful" do
get :new
response.should be_success
end
it "should have the right title" do
get :new
response.should have_selector('title', :content => "Sign up")
end
end
describe "POST 'create'" do
describe "failure" do
before(:each)do
@attr = { :name => "", :email => "", :password => "", :password_confirmation => "" }
end
it "should have the right title" do
post :create, :user => @attr
response.should have_selector('title', :content => "Sign up")
end
it "should render the 'new' page"
post :create, :user => @attr
response.should render_template('new')
end
it "should not create a user" do
lambda do
post :create, :user => @attr
end.should_not change(User, :count)
end
end
end
end