ruby on rails - Nested Form Not Passing Id Attribute on Update -


i using rails5 nested form , nested form below

permitted params

params.require(:audit_type).permit(:name, :total_score, :id,       risk_scoring_attributes: [:low, :medium, :high, :zero_tolerance, :_destroy, :id],       audit_ratings_attributes: [ :id, :from, :to, :description, :_destroy]) 

view

    <%= nested_form_for(@audit_type, method: :patch) |f| %>             <div class="table-responsive">           <table class="table table-search table-striped table-responsive" id="condensedtable">             <thead>               <tr>                 <th>from</th>                 <th>to</th>                 <th>result</th>                 <th>actions</th>               </tr>             </thead>             <tbody id='audit_rating_data'>               <%= f.fields_for :audit_ratings, @audit_type.audit_ratings, :wrapper => false |builder| %>                 <tr class="<%= cycle("odd", "even") -%> number-cell fields">                   <td class="v-align-middle semi-bold">                     <%= builder.number_field :from, placeholder: "#", class: 'from_field', required: true %>                   </td>                   <td class="v-align-middle">                     <%= builder.number_field :to, placeholder: "#", class: 'to_field', required: true %>                   </td>                   <td class="v-align-middle semi-bold">                   <%= builder.text_area :description, class: "text-uppercase", required: true %>                   </td>                   <td class="v-align-middle">                   <%= builder.link_to_remove "<i class='fa fa-trash-o'></i>".html_safe, class: "btn btn-rounded btn-danger" %>                   </td>                 </tr>               <% end %>             </tbody>           </table>           <%= f.link_to_add "<i class='fa fa-plus'></i>".html_safe, :audit_ratings, class: "btn btn-rounded btn-primary", :data => { :target => "#audit_rating_data" }  %>         </div> <% end %> 

and in model have relation

  has_many :audit_ratings   accepts_nested_attributes_for :audit_ratings, allow_destroy: true 

while updating if delete associated records passing parameters as

"audit_ratings_attributes"=>{"0"=>{"from"=>"10", "to"=>"20", "description"=>"test", "_destroy"=>"false"}, "1"=>{"from"=>"10", "to"=>"20", "description"=>"test", "_destroy"=>"1"} } 

what observed missing id attribute on update, comes hidden field nested form. creating new record rather delete.

thanks in advance

solution add hidden field id audit_ratings fields_for :

<%= builder.hidden_field :id, :value => builder.object.id %> 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -