0

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

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

一、形式:

<%= 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 %>

在 Games 控制器中,我有一个 create 方法

def create
    @game = Game.new(params[:game])
    if @game.save
      flash[:success] = "Spiel erfolgreich eingereicht"
      @player_1 = User.find_by_name(@game.player_1)
      @player_1.update_attributes(:games_played => @player_1.games_played + 1)
      redirect_to @game
    else
      render 'new'
    end
 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 

我不明白,为什么用户不应该是有效的。所有测试都通过(以 railstutorial 的方式制定)。我的用户模型:

# == 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

我真的被困在这里,因为这个功能是必不可少的,我必须通过表单做更多的这些记录更新。我真的无法弄清楚为什么我的记录会变得无效。此外,我确信有更好的方法来更新这些记录,甚至可以完全避免这个问题?

提前致谢

4

4 回答 4

1

每当他们是 update_attributes 时,它都会尝试更新密码,这也会使记录无效

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

现在试试

于 2012-09-28T13:31:39.140 回答
0

由于数据库中的数据不佳(某些用户具有相同的电子邮件或名称),它可能无效。如果是这种情况,请重新加载数据库

于 2012-09-28T13:33:38.317 回答
0

根据您的user.errors,您不想在每次保存用户时验证 :password 和 :password_confirmation ;仅当您实际创建用户或允许用户显式更新这些值时,例如从他们的帐户编辑页面。

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

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
于 2012-09-28T13:45:00.163 回答
0

另一种写法。

with_options if: :password do |user|
  user.validates :password, presence: true, length: { minimum: 6 }
  user.validates :password_confirmation, presence: true
end
于 2013-01-25T21:29:40.103 回答