Anubhi Golechha
Anubhi Golechha

Reputation: 167

How to update record in ror?

I have one table employee resignation in which i want to update one column named as settled on date . There is one button in my page when i click on that button one model pop-up on screen for updating the settled on date. But the query which i am using to update is not giving any output.

Form -

<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>

Controller code -

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

Upvotes: 0

Views: 256

Answers (1)

jon snow
jon snow

Reputation: 3072

One thing update_all only works into collection instances like ActiveRecord::Relation

And as you are doing find, its only give you ActiveRecord::Base instance of EmployeeResignation

You need to use update for that not update_all

 @employee_resignation.update(settled_on: @settled_on)

Upvotes: 1

Related Questions