dimanche 12 mai 2019

How to work in Rails with accepts_nested_attributes_for, has_many throug and a form

I'm working on a CRUD with 3 models and accepts_nested_attributes_for, I'm trying to save on bd the record of a model and in the join table besides the respective id's a column wich name is behavior, but I cant't figure out how to do it.

I already have the 3 models involved in addition in which despite showing correctly the information I can not save correctly in the bd, keeping in mind that the process_flow model already has the values ​​in the bd stored.

The Models

class LoanPolicy < ApplicationRecord    
 has_many :loan_policy_process_flows
 has_many :process_flows, through: :loan_policy_process_flows
 accepts_nested_attributes_for :loan_policy_process_flows
end

class ProcessFlow < ApplicationRecord
 has_many :loan_policy_process_flows
 has_many :loan_policies, through: :loan_policy_process_flows
end

class LoanPolicyProcessFlow < ApplicationRecord
 belongs_to :loan_policy
 belongs_to :process_flow
end

The Controller

def new
 @loan_policy = LoanPolicy.new
end

def create
    @loan_policy = LoanPolicy.new(loan_policy_params)
    if @loan_policy.save!
        flash[:success] = 'Política Creada con Exito'
        redirect_to loan_policies_path
    else
        flash[:danger] = "Política No Creada"
        redirecto_to new_loan_policy_path
    end

end

def loan_policy_params
        params.require(:loan_policy).permit(
            :name,
            :description,
            :legal_document_id,
            :status,
            :minimun_range,
            :maximun_range,
            :minimun_days,
            :maximun_days,
            :monthly_interest_rate,
            :external_bureau_required,
            :credit_bureau_id,
            loan_policy_process_flows_attributes: [:process_flow_id, :behavior])
end

The _form.html.erb

<%= form_with(model: loan_policy, local: true) do |form| %>
  ....
  <% ProcessFlow.all.each_with_index do |process_flow, i| %>

   <% if i == 0 %>

     <div class="col-5 col-md-5"><label class="control-label">Proceso</label></div>
     <div class="col-6 col-md-6"><label class="control-label">Comportamiento</label></div>

     <br>

     <div class="col-5 col-md-5">
      <%= check_box_tag "loan_policy[loan_policy_process_flows_attributes][][loan_policy_id]", process_flow.id, @loan_policy.process_flow_ids.include?(process_flow.id), id: dom_id(process_flow) %>
      <%= label_tag dom_id(process_flow), process_flow.process_description %>
     </div>

     <div class="col-6 col-md-6">
      <%= select_tag "loan_policy[loan_policy_process_flows_attributes][][behavior]", options_for_select([['Seleccione Comportamiento', nil], ['Manual', 'Manual'], ['Automático', 'Automático']], selected: "loan_policy[process_flow_ids][behavior]" ), class: 'form-control', id: "process_flow_select_picker_"+dom_id(process_flow)%>
     </div>

   <% else %>

     <br>
     <div class="col-5 col-md-5">
      <%= check_box_tag "loan_policy[loan_policy_process_flows_attributes][][loan_policy_id]", process_flow.id, @loan_policy.process_flow_ids.include?(process_flow.id), id: dom_id(process_flow) %>
      <%= label_tag dom_id(process_flow), process_flow.process_description %>
     </div>

     <div class="col-6 col-md-6">
      <%= select_tag "loan_policy[loan_policy_process_flows_attributes][][behavior]", options_for_select([['Seleccione Comportamiento', nil], ['Manual', 'Manual'], ['Automático', 'Automático']], selected: "loan_policy[process_flow_ids][behavior]" ), class: 'form-control', id: "process_flow_select_picker_"+dom_id(process_flow)%>
      </div>

   <% end %>

 <% end %>

<% end %>

The parameters that are being sent:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"bH2xYTIhjLMDqBW1YRm5vaSUxY2Uc1QXA2FwLfQ0lEQ9d9EM5NovPZai7xPQwpXjBLH4KYS5Za/yazAtoyn+bA==", "loan_policy"=>{"name"=>"Policita 1", "status"=>"true", "legal_document_id"=>"1", "minimun_range"=>"120", "maximun_range"=>"2200", "minimun_days"=>"25", "maximun_days"=>"60", "monthly_interest_rate"=>"0.3", "credit_bureau_id"=>"1", "external_bureau_required"=>"Si", "loan_policy_process_flows_attributes"=>[{"loan_policy_id"=>"1", "behavior"=>"Manual"}, {"loan_policy_id"=>"2", "behavior"=>"Automático"}, {"behavior"=>""}, {"behavior"=>""}, {"behavior"=>""}, {"behavior"=>""}, {"behavior"=>""}]}, "commit"=>"Continuar"}

Additionally, console is also shown

Unpermitted parameter: :loan_policy_id
Unpermitted parameter: :loan_policy_id

what I would also like to do in loan_policy_process_flows_attributes would be a hash with the ids that were selected and the behavior of the respective select




Aucun commentaire:

Enregistrer un commentaire