如何在ror中更新记录?

时间:2017-10-14 06:45:19

标签: ruby-on-rails

我有一名员工辞职,其中我想更新一个名为“已确定日期”的列。当我点击该按钮时,我的页面中有一个按钮,屏幕上会显示一个模型弹出窗口,用于更新已结算日期。但是我用来更新的查询没有给出任何输出。

表格 -

<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h4 class="modal-title"><center> Settelment Date </center></h4>
    </div>
    <div class="modal-body">
     <%= bootstrap_form_for :employee_resignation,url:{action:"settelment_date", id: @employee_resignation.id} do |f| %>

    <div class="row">
      <div class="col-sm-6">
        <div class="field">
            <%= f.text_field :settled_on,label: "Settled On", class: 'settled_on'%>
          </div>
      </div>
     <div class="col-sm-6" style="padding-top:20px;">
        <%= f.submit "Update", class: 'btn btn-sm btn-warning fa fa-modx' %>

      </div>
    </div>
      <% end %>

</div>
<div class="modal-footer"></div>
</div>
</div>  


<script type="text/javascript">
  $(function(){
    $('.settled_on').datepicker({
    changeYear:true,
    changeMonth: true,
    yearRange: '-1:+150',
    dateFormat: 'dd-mm-yy' });

  }); 
</script>

控制器代码 -

 def settelment_date
     @settled_on = params[:settled_on]   
     @employee_resignation = EmployeeResignation.find(params[:id])
     @employee_resignation.update_all(settled_on: @settled_on)
  end

1 个答案:

答案 0 :(得分:1)

有一件事update_all仅适用于ActiveRecord::Relation

等集合实例

正如您所做的那样find,它只会为ActiveRecord::Base <{1}}实例提供EmployeeResignation

您需要update使用update_all

 @employee_resignation.update(settled_on: @settled_on)