2

在此先感谢您的帮助!

我刚刚完成 Rails 教程的第 6 章:

http://ruby.railstutorial.org/chapters/modeling-users#sec-6_4

我跑了:

❤ git checkout master

一切顺利,然后我跑了:

❤ git merge modeling-users

并得到以下错误:

Updating fc9f72a..90d1ba6
error: The following untracked working tree files would be overwritten by merge:
        app/models/user.rb
Please move or remove them before you can merge.
Aborting

这是我的 app/models/user.rb 文件:

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

现在我的 Sublime Text 2 user_spec.rb 和 application_helper_spec.rb 文件不会保存并出现以下错误:

无法保存 ~/code/rails_projects/sample_app/spec/models/user_spec.rb

user_spec.rb:

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com", 
                     password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " * 51 }
    it { should_not be_valid }
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@bax.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end
      end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end      
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it { should_not be_valid }
  end

  describe "when password doesn't match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

    describe "with valid password" do
      it { should == found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end
end

和我的 application_helper_spec.rb 文件:

require 'spec_helper'

describe ApplicationHelper do

  describe "full_title" do
    it "should include the page title" do
      full_title("foo").should =~ /foo/
    end

    it "should include the base title" do
      full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
    end

    it "should not include a bar for the home page" do
      full_title("").should_not =~ /\|/
    end
  end
end
4

1 回答 1

3

发生错误The following untracked working tree files would be overwritten by merge是因为app/models/user.rb文件夹中有一个文件当前未添加到任何分支。

git status您可以通过运行命令来验证这一点;它应该显示类似于以下内容的输出:

$ git status
# On branch master
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   app/models/user.rb
no changes added to commit (use "git add" and/or "git commit -a")

当您运行时git merge modeling-users,它会尝试将分支合并modeling-usersmaster分支。该modeling-users分支已经包含了一个副本app/models/user.rb。合并命令使它用modeling-users分支中已经存在的文件覆盖文件的未跟踪版本。因此,错误消息会警告您可能会丢失您想要保留的东西。

如消息所示,通过运行命令git status将文件添加到主分支。git add app/models/user.rb

Unable to save ~/code/rails_projects/sample_app/spec/models/user_spec.rb消息似乎与此无关。从错误消息中无法得出什么结论;也许其他地方有更多信息。

关闭项目并重新打开它可能会有所帮助。您可能还想尝试将文件保存在不同的编辑器中(也许是 vim?)

于 2013-02-07T03:23:12.187 回答