日期字段随机不在模型中更新

时间:2013-12-23 14:35:56

标签: ruby-on-rails date

我使用的是Rails 4.0.2和Ruby 2.0.0。我有两个日期字段(performance_start和performance_end)。当我在这些字段中输入值时,它们会与其他参数一起提交,但更新查询会随机排除这些字段或将它们包含在更新查询中。

知道为什么会这样吗?我似乎无法弄清楚为什么会这样做。任何帮助将不胜感激。

这是我在提交此表单时在命令提示符中看到的输出示例(请注意,在此示例中只更新了一个日期,即使两者都已传入):

Processing by LapsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ol2312IJT3om5ihpIWeLfGziLMlgbDU5bRllMTi76KM=", "lap"=>{"staff_id"=>"1", "activity_id"=>"1", "person_id"=>"1", "job_title"=>"Software Developer", "performance_start"=>"12/01/2013", "performance_end"=>"12/31/2013", "other_billing_code"=>"", "project_labor_code"=>"", "paid_from_field"=>"0", "will_any_of_this_work_occur_in_us"=>"0", "billing_rate_currency"=>"", "billing_rate"=>"", "billing_rate_frequency"=>"Male", "billable_days"=>"", "notes"=>"testing", "aou_number"=>"", "aou_identity_black"=>"0", "aou_identity_native"=>"0", "aou_identity_south_asian"=>"0", "aou_identity_hispanic"=>"0", "aou_identity_asian_pacific"=>"0", "aou_is_veteran"=>"0", "aou_veteran_status_selection"=>"0", "aou_is_service_disabled"=>"0", "aou_emergency_contact_name"=>"", "aou_emergency_contact_address_line1"=>"", "aou_emergency_contact_phone"=>"", "aou_home_contact_name"=>"", "aou_home_address"=>"", "aou_home_phone"=>""}, "id"=>"5"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  Person Load (0.2ms)  SELECT "people".* FROM "people" WHERE "people"."user_id" = $1 ORDER BY "people"."id" ASC LIMIT 1  [["user_id", 1]]
  Lap Load (0.5ms)  SELECT "laps".* FROM "laps" WHERE "laps"."id" = $1 LIMIT 1  [["id", "5"]]
   (0.2ms)  BEGIN
  SQL (0.7ms)  UPDATE "laps" SET "notes" = $1, "performance_start" = $2, "updated_at" = $3 WHERE "laps"."id" = 5  [["notes", "testing"], ["performance_start", Sat, 12 Jan 2013], ["updated_at", Mon, 23 Dec 2013 14:27:54 UTC +00:00]]
   (0.4ms)  COMMIT

以下是我的表单中的两个表单字段

<%= simple_form_for(@lap, :html => { role: "form"}) do |f| %>

<%= f.input :performance_start, :label => false, :wrapper => :append, :class => "form-group" do %>
    <label class="col-sm-4 control-label no-padding-right"> Performance Start </label>
    <div class="col-sm-2">
      <%= f.input_field :performance_start, as: :string, :required => true, :class => "col-xs-7 col-sm-5 datepicker" %>
    </div>
    <div class="col-sm-6">
      <span class="help-button" data-rel="popover" data-trigger="hover" data-placement="left" data-content="mm/dd/yyyy" title="Date Format">?</span>
    </div>
<% end %>

<%= f.input :performance_end, :label => false, :wrapper => :append, :class => "form-group" do %>
    <label class="col-sm-4 control-label no-padding-right"> Performance End </label>
    <div class="col-sm-2">
      <%= f.input_field :performance_end, as: :string, :required => true, :class => "col-xs-7 col-sm-5 datepicker" %>
    </div>
    <div class="col-sm-6">
      <span class="help-button" data-rel="popover" data-trigger="hover" data-placement="left" data-content="mm/dd/yyyy" title="Date Format">?</span>
    </div>
<% end %>

这是脚手架控制器中的更新方法:

def update
  @lap = Lap.find(params[:id])
  respond_to do |format|
    if @lap.update(lap_params)
      if @lap.status.blank?
        @lap.status = "Draft"
      end
      format.html { redirect_to @lap, notice: 'Lap was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @lap.errors, status: :unprocessable_entity }
    end
  end
end

该表的架构如下:

create_table "laps", force: true do |t|
  ...
  t.date     "performance_start"
  t.date     "performance_end"
  ...
end

lap_params方法是我控制器中的私有方法,如下所示(省略不相关的字段):

def lap_params
  params.require(:lap).permit(..., :performance_start, :performance_end, ...)
end

1 个答案:

答案 0 :(得分:0)

假设您实际上已在performance_startperformance_end的表单中输入了数据,我猜想原始记录中没有修改过一个(或两个)参数,所以有没有理由让rails更新它。