我的Rails應用程序中有一個表單元素,用於更改帖子的發佈日期(而不是時間)。我這樣做,所以我可以使用jquery-ui中的datepicker元素,並有時間下拉菜單。保存updated_at的日期屬性
我想更新「的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 %>
steps_controller:
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日期?
看起來像'已發佈'可能在您的'更新'行動中爲零。 – varatis
如果我決定只使用published_on屬性,我如何分別更新日期和時間(因爲我有日期和下拉菜單的文本字段?) – scientiffic
如果您打印出'String(params [ :published_on])'在你的控制器中,你會得到什麼? – zeantsoi