我有一个def show
,每次查看它时都会更新我的用户模型。挑战在于我想更新特定属性而不更新所有内容。目前我更新了@user.year
和@user.today_steps
Rails将模型与创建模型分开保存
当我尝试保存@user.save
时发生错误。
p @user.errors.full_messages
返回["Password is too short (minimum is 6 characters)"]
但我没有试图更新密码。如何在不更新所有属性的情况下保存和更新某些属性?这是一个挑战的原因是因为我的模型需要验证一些属性。
class User < ActiveRecord::Base
# attr email, name, points
before_save { self.email = email.downcase }
before_create :create_remember_token
attr_accessible :email, :name, :password, :password_confirmation, :year, :today_steps
has_many :competitions, dependent: :destroy
has_many :attendees, :dependent => :destroy
has_many :competitions, :through => :attendees
has_one :device
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
serialize :year, Array
你的'show'动作是什么样的? – zeantsoi
操作代码将非常有用,因为不清楚在更新记录或保存记录时是否发生错误。 –
@ Mhsmith21 - 除非您提供更多细节,否则我们无法为您提供帮助。 – zeantsoi