我的 Rails 应用程序中有一个表单元素,用于更改帖子的发布日期(而不是时间)。我这样做是为了可以使用 jquery-ui 中的 datepicker 元素,并在当时有下拉菜单。
我想将“updated_at”属性更新为发布日期。但是,我收到一个错误:
PG::Error: ERROR: null value in column "updated_at" violates not-null constraint
: UPDATE "steps" SET "updated_at" = NULL WHERE "steps"."id" = 472
这是我的表格(可以正确保存时间但不能保存日期):
<%= semantic_form_for [@project, @step], :html => {:multipart => true} do |f| %>
<%= f.inputs do%>
<div id="publishedOn">
<span class="label" style="margin-left:9px">Published on</span>
<%= f.text_field :published_on, :value=> (@step.updated_at.strftime("%m/%d/%Y")) %> at <%= f.time_select :updated_at, :class=>"btn dropdown-toggle", :ampm=> true, :ignore_date=>true%>
</div>
<% end %>
步骤控制器:
def update
@step = @project.steps.find_by_position(params[:id])
published = String(params[:published_on])
published = published.gsub("/", "-")
@step.update_attributes(:updated_at => published)
end
步骤.rb:
class Step < ActiveRecord::Base
extend FriendlyId
friendly_id :position
has_ancestry :orphan_strategy => :adopt
attr_accessible :description, :name, :position, :project_id, :images_attributes, :parent_id, :ancestry, :updated_at, :published_on
belongs_to :project
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :allow_destroy => :true
validates :name, :presence => true
end
我是否需要重新格式化 published_on 日期参数以更新 updated_at 日期?