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

新的.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 中的创建事件。由于您(还)没有任何代码,Rails 假设您只想渲染create.html.erb,它不存在(并且永远不会存在)。为了让它立即工作,你必须在redirect_to @user下面添加if @user.save. 检查清单 7.25

于 2012-04-18T17:44:22.820 回答
0

跳到教程底部,问题得到解答。redirect_to @user在您的#Handle 下包含成功保存。

于 2013-08-15T10:00:41.597 回答