为什么在模型中更新表单擦除字段

时间:2015-09-08 22:06:03

标签: ruby-on-rails controller

我有一个名为bill的表单,上面有不同的账单名称。表单设置为如果没有账单存在占位符,但如果账单存在则显示账单名称。表格看起来像这样

bill.html.erb

<%= form_for [:users, @bill], :validate => true do |f| %>
  <div class="row">
    <div class="col-xs-6 form-group">
      <% if bill.cell_phone.blank?  %>
        <label> Cell Phone Company Name</label>
          <%= f.text_field :cell_phone, autofocus: true, class: 'form-control', placeholder: "Enter your cell phone company name as it shows on your bank statement" %>
     <% else %>
       <label>Current Cell Phone Company Name:</label>
          <%= bill.cell_phone %>
            <%= f.text_field :cell_phone, autofocus: true, class: 'form-control', placeholder: "Update your cell phone company name as it shows on your bank statement" %>
      <% end %>
    </div>
<div class="col-xs-6 form-group">
      <% if bill.gas_name.blank?  %>
        <label> Heating Gas Company Name</label>
          <%= f.text_field :gas_name, autofocus: true, class: 'form-control', placeholder: "Enter your heating gas company name as it shows on your bank statement" %>
      <% else %>
          <label> Current Heating Gas Company Name:</label>
            <%= bill.gas_name %>
            <%= f.text_field :gas_name, autofocus: true, class: 'form-control', placeholder: "Update your heating gas company name as it shows on your bank statement" %>
      <% end %>
    </div>

我的问题是出于某种原因,如果我更改其中一个账单并尝试更新它会删除其他账单并仅更新新账单。我希望能够更新一个账单并保留其他账单。即使在我的服务器日志中它显示进入更新的所有账单。这是日志...

Started PATCH "/users/bills/...400000a" for 10.0.2.2 at 2015-09-08 17:57:38 +0000
Processing by Users::BillsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xlzypBl1o3+iqAMROYUnjAuTR3TUIgSzyIA4EaVRkMw=", "bill"=>{"cell_phone"=>"Virgin", "gas_name"=>"nicor", "rent_name"=>"wells fargo", "electric_bill"=>"comed", "water_bill"=>"city of chicago", "cable_bill"=>"direc tv", "insurance"=>"american family", "current_rent_pmt"=>"1325"}, "commit"=>"Update Bill Payments", "id"=>"...400000a"}
D, [2015-09-08T17:57:38.851608 #11316] DEBUG -- :   MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.9856ms
D, [2015-09-08T17:57:38.853790 #11316] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=sample_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('...1000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 2.0296ms
D, [2015-09-08T17:57:38.856265 #11316] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=sample_development collection=bills selector={"$query"=>{"tenant_id"=>BSON::ObjectId('...1000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.6492ms
D, [2015-09-08T17:57:38.860914 #11316] DEBUG -- :   MOPED: 127.0.0.1:27017 UPDATE       database=sample_development collection=bills selector={"_id"=>BSON::ObjectId('...400000a')} update={"$set"=>{"cell_phone"=>"virgin", "gas_name"=>nil, "rent_name"=>nil, "electric_bill"=>nil, "water_bill"=>nil, "cable_bill"=>nil, "insurance"=>nil}} flags=[]
D, [2015-09-08T17:57:38.861626 #11316] DEBUG -- :                          COMMAND      database=sample_development command={:getlasterror=>1, :w=>1} runtime: 0.9025ms
Redirected to http://0.0.0.0:3000/users/dashboard
Completed 302 Found in 16ms (ActiveRecord: 0.0ms)

最后,这是我的bills_controller更新方法(似乎工作正常)

def update
    @user = current_user
    if current_user.bill.update_attributes(bill_params)
      TransAggregationWorker.perform_async(@user.id.to_s)
      redirect_to users_dashboard_path, :notice => "Payment Informaton Updated"
    else
      redirect_to users_dashboard_path, :notice => "Payment Information was not updated, please try again"
    end
  end

-UPDATE -

这是bill params

def bill_params
    params.require(:bill).permit( :cell_phone, :gas_name, :rent_name, :electric_bill, :water_bill, :cable_bill, :insurance, :current_rent_pmt)
  end

0 个答案:

没有答案