无法更新模型记录

时间:2014-06-13 20:04:55

标签: ruby-on-rails forms model controller

我有以下表单更新我的Pack模型:

= form_for(@pack) do |f|
    .form-group
        = f.label :amount
        = f.text_field :amount, :autofocus => true, class: 'form-control'
    = f.submit 'Submit', :class => 'button right'

在我的模型pack.rb中,我在:created_on上进行了唯一性验证,这似乎阻止了我的更新通过。这是我的模特:

#
#  id         :integer          not null, primary key
#  created_on :date
#  user_id    :integer
#  created_at :datetime
#  updated_at :datetime
#  amount     :integer
#

class Pack < ActiveRecord::Base
    belongs_to :user

    default_scope -> { order('created_on DESC') }
    scope :today, -> { where(:created_at => (Time.now.beginning_of_day..Time.now)) }
    scope :week, -> { where(:created_at => (Time.now.beginning_of_week..Time.now))}
    scope :month, -> { where(:created_at => (Time.now.beginning_of_month..Time.now))}
    scope :year, -> { where(:created_at => (Time.now.beginning_of_year..Time.now))}

    validates :user_id, presence: true
    validates :created_on, uniqueness: true
    validates :amount, presence: true, length: { maximum: 1 }
end

当我删除唯一性验证时,编辑没有任何问题。该{1}}也不会更改该记录。如何在保持唯一性属性的同时保存记录?

0 个答案:

没有答案