如何更新ror中的记录?

问题描述:

我有一个表员工辞职,其中我想更新一列名为定居日期的列。当我点击该按钮时,屏幕上会出现一个按钮,用于更新结算日期。但是我用来更新的查询没有提供任何输出。如何更新ror中的记录?

表 -

<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 

一件事update_all只能到集合实例像ActiveRecord::Relation

而当你在做find,其只给你ActiveRecord::Base实例的EmployeeResignation

您需要使用update对于不update_all

@employee_resignation.update(settled_on: @settled_on) 
+0

但她通过POST提交表单,所以不会'@ settled_on'为空或零? –

+0

POST提交效果如何?但AFA的数据来自'params [:settled_on]'。由于问题提示'如何更新',那么我不会担心这些事情。 –

+0

@settled_on = params [:settled_on]我想在这个价值如何,我可以得到? –