0

我正在关注 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
4

3 回答 3

6
def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to :action => :index
  else
    @title = "Sign up"
    render 'new'
  end
end
于 2013-01-29T16:07:25.987 回答
4

Rails 期望有一个名为 create.html.erb 的文件,它会在您成功创建用户时呈现。

如果您不想拥有 create.html.erb 文件,则必须呈现不同的操作或重定向。这需要在您有“#Handle a successful save”的地方进行。

于 2013-01-29T16:00:27.497 回答
2

感谢您的建议。

实际问题是渲染后我有一个“=”符号。它显示在他们的文件中,但是当我快进视频时,我注意到它不存在。

再次感谢。

于 2013-01-30T13:25:57.110 回答