Ruby on rails 更新数据库中的记录会使记录无效吗?

Ruby on rails 更新数据库中的记录会使记录无效吗?,ruby-on-rails,model,console,record,updating,Ruby On Rails,Model,Console,Record,Updating,所以,这让我发疯,我发现自己既不能自己回答问题,也不能阅读其他问题和答案 当我尝试更新通过webapp创建的数据库中的记录时,我无法更改该记录的值。在这种情况下,用户需要填写一个表单,然后应用程序应该更新不同模型中的多个字段 首先,形式: <%= form_for(@game) do |g| %> <%= g.label :player_1 %> <%= g.text_field :player_1 %> <%= g.label :play

所以,这让我发疯,我发现自己既不能自己回答问题,也不能阅读其他问题和答案

当我尝试更新通过webapp创建的数据库中的记录时,我无法更改该记录的值。在这种情况下,用户需要填写一个表单,然后应用程序应该更新不同模型中的多个字段

首先,形式:

<%= form_for(@game) do |g| %>
  <%= g.label :player_1 %>
  <%= g.text_field :player_1 %>

  <%= g.label :player_2 %>
  <%= g.text_field :player_2 %>

  <%= g.label :faction_1 %>
  <%= g.text_field :faction_1 %>

  <%= g.label :faction_2 %>
  <%= g.text_field :faction_2 %>

  <%= g.label :caster_1 %>
  <%= g.text_field :caster_1 %>

  <%= g.label :caster_2 %>
  <%= g.text_field :caster_2 %>

  <%= g.label :point_level %>
  <%= g.text_field :point_level %>

  <%= g.label :winner %>
  <%= g.text_field :winner %>

  <%= g.submit "eintragen", class: "btn btn-large btn-primary" %>
<% end %>
不用说,它不像我预期的那样工作。困扰我的是:如果我试图在控制台中更改记录,会发生以下情况:

user = User.find_by_name("Example User")
User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."name" = 'Example User' LIMIT 1
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2012-09-28 11:48:10", updated_at: "2012-09-28 11:48:10", password_digest: "$2a$10$3GQnsHGAfp09v1v.csb6Ce8pCPwfY0Hl1nG2a09BvOo8...", remember_token: "MsRfXle0N67ws9otkeDP_w", admin: true, elo_rating: 1000, games_played: 0> 


user.valid?
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."name") = LOWER('Example User') AND "users"."id" != 1) LIMIT 1
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('example@railstutorial.org') AND "users"."id" != 1) LIMIT 1
=> false 
我不明白,为什么用户不应该是有效的。所有试验均通过铁路结构规定的试验。我的用户模型:

# == 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
#  password_digest :string(255)
#  remember_token  :string(255)
#  admin           :boolean          default(FALSE)
#  elo_rating      :integer          default(1000)
#  games_played    :integer          default(0)
#

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

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token
  validates :name,  presence: true, 
                    uniqueness: true, 
                    length: { :maximum => 50 }

  # regex take from railstutorial by Michael Hartl (http://ruby.railstutorial.org)
  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
  validates :games_played, presence: true
  validates_numericality_of :games_played, greater_than_or_equal_to: 0
  validates_numericality_of :elo_rating, greater_than_or_equal_to: 0

  private

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
end
我真的被困在这里了,因为这个功能是必不可少的,我必须通过表单进行更多的记录更新。我真的不明白为什么我的记录会失效。此外,我相信有更好的方法来更新这些记录,甚至可以完全避免这个问题


提前感谢

每当他们的is更新属性时,itt也会尝试更新密码,这将使记录无效

validates :password, presence: true, length: { minimum: 6 }, :if => :password
validates :password_confirmation, presence: true, :if => :password_confirmation

现在试试它

也许它无效,因为数据库中的数据很差,有些用户有相同的电子邮件或名称。如果是这种情况,请根据您的用户重新加载数据库。错误:您不希望在每次保存用户时验证:密码和:密码\u确认;仅当您实际创建用户或允许用户显式更新这些值时,例如从他们的帐户编辑页面

如果进行如下更改,您的用户模型验证逻辑可能会起作用:

class User < ActiveRecord::Base
  validates :password, presence: {if: :password_required?}, length: { minimum: 6, allow_blank: true }
  validates :password_confirmation, presence: { if: :password_required? }

  private

  def password_required?
    !persisted? || !password.nil? || !password_confirmation.nil?
  end
end

另一种写作方式

with_options if: :password do |user|
  user.validates :password, presence: true, length: { minimum: 6 }
  user.validates :password_confirmation, presence: true
end

您的用户模型上有哪些验证错误?没有显式错误。它只返回有效值?=>falseAfter user.valid?,user.errors.inspect的输出是什么?@rossta user.errors.inspect=>[\can't blank\,\is too short minimal is 6个字符\],:password\u confirmation=>[\can't blank\]}>所有数据库条目都是唯一的,很遗憾,这不是问题。只需在user.valid之后键入user.errors即可?告诉你所看到的验证:密码确认,状态:真,:如果=>:password\u确认用旧的changeu.errors.inspect=>[\cannot be blank\]}>好的,我现在看到问题了,但是如何解决它?在前面的评论中告诉了你解决方案我想是的,非常感谢!不过还有一件事:不,我必须在每次提交游戏时登录,尽管我已经登录了。我不明白。。。抱歉问了这么愚蠢的问题谢谢,不幸的是,当我使用这段代码时,如果我访问一个页面,我会得到以下错误:未定义的方法密码\u required?'for。我错过了什么?第1行周围提取的源代码:`1:2:3:Willkommen Zureueck bei dicedown 4:看起来我打错了一行验证代码;我的意思是:需要密码吗?在这两个选项中:显示选项。刚刚编辑了样本
with_options if: :password do |user|
  user.validates :password, presence: true, length: { minimum: 6 }
  user.validates :password_confirmation, presence: true
end