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



我正在使用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

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

最新更新