2

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

問題が何であるかはわかりません。コードを何度か調べて、本の例と一致することを確認しました。私はそれが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

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>

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
4

2 に答える 2

8

短い答え:

(まだ)機能しないはずです。チュートリアルを続けると、答えはセクション7.4にあります。

少し長い回答:

フォームに入力して送信ボタンを押すと、users_controllerのcreate-eventがトリガーされます。そこには(まだ)コードがないため、Railsは、レンダリングしたいだけであると想定しますがcreate.html.erb、これは存在しません(そして決して存在しません)。これをすぐに機能させるには、のredirect_to @userすぐ下に追加する必要がありますif @user.saveリスト7.25を確認してください;

于 2012-04-18T17:44:22.820 に答える
0

チュートリアルの一番下にジャンプすると、質問に対する回答が表示されます。成功した保存を#Handleredirect_to @userのすぐ下に含めます。

于 2013-08-15T10:00:41.597 に答える