Hartl Ruby-on-Rails教程(3.2),第7.2节 - 注册表单缺少模板

use*_*808 2 ruby-on-rails

我在测试ruby-on-rails教程的注册页面时遇到以下错误:

Failures:

1) User pages signup with valid information should create a user
 Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
 ActionView::MissingTemplate:
   Missing template users/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
     * "/Users/Fif/rails_projects/sample_app/app/views"
 # (eval):2:in `click_button'
 # ./spec/requests/user_pages_spec.rb:43:in `block (5 levels) in <top (required)>'
 # ./spec/requests/user_pages_spec.rb:43:in `block (4 levels) in <top (required)>'

Finished in 0.76918 seconds
35 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:42 # User pages signup with valid information should create a user
Run Code Online (Sandbox Code Playgroud)

我不确定问题是什么,我已经多次查看代码以确保它与书中的示例相符.我很确定它与new.html.erb文件有关

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
    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
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

new.html.erb

<%= provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

users_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      # Handle a successful save.
    else
      render 'new'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

kla*_*eck 8

简答:

它不应该工作(还).继续本教程,答案将在第7.4节中介绍;

稍微长一点答案:

当您填写表单并按下提交按钮时,users_controller中的create-event将被触发.由于你还没有任何代码,Rails假定你只想渲染create.html.erb,这是不存在的(永远不会).为了使其立即生效,您必须在redirect_to @user下方添加if @user.save.检查清单7.25 ;