所以,这让我发疯,我发现自己既不能自己回答这个问题,也不能通过阅读其他问题和答案来回答这个问题。
当我尝试更新通过 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
我真的被困在这里,因为这个功能是必不可少的,我必须通过表单做更多的这些记录更新。我真的无法弄清楚为什么我的记录会变得无效。此外,我确信有更好的方法来更新这些记录,甚至可以完全避免这个问题?
提前致谢