模板缺少用户/创建

时间:2013-01-29 14:56:10

标签: ruby-on-rails

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

New.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

3 个答案:

答案 0 :(得分:6)

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to :action => :index
  else
    @title = "Sign up"
    render 'new'
  end
end

答案 1 :(得分:4)

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

如果您不想拥有create.html.erb文件,则必须呈现不同的操作或重定向。这需要进入“#处理成功保存”的位置。

答案 2 :(得分:2)

感谢您的建议。

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

再次感谢。