datetime_select ヘルパーから送信された datetime 値の読み込みに取り組んでいるので、既存の保存されたデータと比較できます。
次のコードを使用していますが、少し面倒です。
DateTime.parse("#{params['start_date(1i)'].to_i}-#{params['start_date(2i)'].to_i}-#{params['start_date(3i)'].to_i} #{params['start_date(4i)'].to_i}:#{params['start_date(5i)'].to_i} -500")
日時オブジェクトを作成する簡単な方法はありますか?
編集:
ここに私のフォームコードがあります:
= form_for(@charity) do |f|
- if @charity.errors.any?
#error_explanation
%h2
= pluralize(@charity.errors.count, "error")
prohibited this charity from being saved:
%ul
- @charity.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name, 'Name *'
= f.text_field :name
%fieldset
%legend Dates in Eastern Standard Time
.field
= f.label :start_date, 'Start Date *'
= f.datetime_select :start_date, :default => DateTime.now
.field
= f.label :end_date, 'End Date *'
= f.datetime_select :end_date, :default => DateTime.now
.field
= f.label :percentage, 'Percentage of sales to be donated *'
= f.text_field :percentage
.field
= f.label :charity_number, 'The identification number for your charity *'
= f.text_field :charity_number
.field
Donation options:
%span{ title: "Dollar amounts that you wish to allow people to donate on top of their order.", class: "tooltip_hover" }= image_tag "question.png"
- DonationOption.all.each do |option|
= f.label option.id, number_to_currency(option.amount, unit: "$"), class: "donation_label"
= check_box_tag "charity[donation_option_ids][]", option.id, @charity.donation_option_ids.include?(option.id), class: "donation_check"
.actions
= f.submit nil, class: "btn-primary"
コントローラ:
def update
@charity = Charity.find(params[:id])
respond_to do |format|
if @charity.update_values(params[:charity])
format.html { redirect_to @charity, notice: 'Charity was successfully updated.', :charity => @charity }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @charity.errors, status: :unprocessable_entity }
end
end
end
update_values メソッド:
def update_values(attributes)
s_date = DateTime.parse("#{attributes['start_date(1i)'].to_i}-#{attributes['start_date(2i)'].to_i}-#{attributes['start_date(3i)'].to_i} #{attributes['start_date(4i)'].to_i}:#{attributes['start_date(5i)'].to_i} -500")
attributes.delete('start_date(1i)')
attributes.delete('start_date(2i)')
attributes.delete('start_date(3i)')
attributes.delete('start_date(4i)')
attributes.delete('start_date(5i)')
# if the charity has not started yet
if DateTime.now.utc < self.start_date.utc
self.start_date = s_date
attributes.merge!(reset_create_job)
end
e_date = DateTime.parse("#{attributes['end_date(1i)'].to_i}-#{attributes['end_date(2i)'].to_i}-#{attributes['end_date(3i)'].to_i} #{attributes['end_date(4i)'].to_i}:#{attributes['end_date(5i)'].to_i} -500")
attributes.delete('end_date(1i)')
attributes.delete('end_date(2i)')
attributes.delete('end_date(3i)')
attributes.delete('end_date(4i)')
attributes.delete('end_date(5i)')
if DateTime.now.utc < self.end_date.utc
self.end_date = e_date
attributes.merge!(reset_delete_job)
end
self.update_attributes(attributes)
end