2017-10-14 76 views
0

我有一個表員工辭職,其中我想更新一列名爲定居日期的列。當我點擊該按鈕時,屏幕上會出現一個按鈕,用於更新結算日期。但是我用來更新的查詢沒有提供任何輸出。如何更新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 

回答

1

一件事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]我想在這個價值如何,我可以得到? –