Problem to update a hash array

1

Good morning, I have a problem creating a form that allows me to update several models, I'm using the create method to update the models, the detail is that the form sends me the parameters belonging to a payment arrangement (payrolls) , one line (tr) for each payment. However in the console I get the following error:

  

Unpermitted parameters: employee_wd_id, employee_wd_attributes

And I have the following configuration in the controller:

def payroll_params
  params.permit( 
    payroll: [
      :id, :employee_id, :week_id, :department_id, :dias_trabajados,
      :dias_vacaciones, :faltas, :total_percepciones, :total_retenciones,
      :total_sueldo_bruto, :total_sueldo_neto, :sueldo_fiscal, :pago_impuesto,
      :pago_eps, :tipo_pago_fiscal, :tipo_pago_eps, :created_by, :updated_by,
      employee_wd_attributes: [:id, :jueves, :viernes, :sabado, :domingo, :lunes, :martes, :miercoles],
      employee_wad_attributes: [:id,:importe_ads]
    ]
  )
end

In the model:

class Payroll < ApplicationRecord
  belongs_to :employee
  belongs_to :department
  belongs_to :week
  belongs_to :employee_cw
  has_many   :employee_wad, :dependent=> :destroy 
  has_one    :employee_wd, :dependent=> :destroy

  accepts_nested_attributes_for :employee_wad      
  accepts_nested_attributes_for :employee_wd
end

And in the view I use the following format (in summary form):

<%= form_tag(controller: "payrolls", action: "create",method:"post", remote: true) do %>   

  <% @payroll.each do |payroll| %>
    <tr><td>
      <%= text_field_tag("payroll[][employee_wad_attributes][importe_ads]", ad[0]) %>
    </td></tr>
  <% end %>

  <tr><td><%= submit_tag("guardar") %></td></tr>
<% end %>

At this point I would like to mention that what I want is an arrangement of amounts_ads belonging to employee_wad_attributes since there are approximately 12 concepts currently that I must update and all are sent from the payments view.

In the update method:

def update
  payroll_params[:payroll].each do |upd_params|
    params_employeewd = {
      :jueves=>upd_params[:jueves],
      :viernes=>upd_params[:viernes],
      :sabado=>upd_params[:sabado],
      :domingo=>upd_params[:domingo],
      :lunes=>upd_params[:lunes],
      :martes=>upd_params[:martes],
      :miercoles=>upd_params[:miercoles]
    }

    params_payroll = {
      :employee_id=>upd_params[:employee_id], 
      :week_id=>upd_params[:week_id], 
      :department_id=>upd_params[:week_id], 
      :dias_trabajados=>upd_params[:week_id],
      :dias_vacaciones=>upd_params[:week_id],
      :faltas=>upd_params[:week_id],
      :total_percepciones=>upd_params[:week_id],
      :total_retenciones=>upd_params[:week_id],
      :total_sueldo_bruto=>upd_params[:week_id],
      :total_sueldo_neto=>upd_params[:week_id],
      :sueldo_fiscal=>upd_params[:week_id],
      :pago_impuesto=>upd_params[:week_id],
      :pago_eps=>upd_params[:week_id],
      :tipo_pago_fiscal=>upd_params[:week_id],
      :tipo_pago_eps=>upd_params[:week_id]
    }

    @employeeWd = EmployeeWd.find(upd_params[:employee_wd_id])
    @employeeWd.update(params_employeewd)

    @payroll = Payroll.find(upd_params[:id])
    @payroll.update(params_payroll)
  end      
end

I really do not know if I'm getting the parameters wrong or I do not know, but in addition to sending me all the parameters are not separating them but I'm running them in a single hash, for example if they are 2 lines when the method update for a second time the parameters go null.

first time:

  

Payroll Load (0.5ms) SELECT payrolls . * FROM payrolls WHERE payrolls . id = 258 LIMIT 1

second time:

  

Payroll Load (0.5ms) SELECT payrolls . * FROM payrolls WHERE payrolls . id = NULL LIMIT 1

I thank you in advance for the help you can give me.

  

Edited:

In the logs I send this:

Started POST "/companies/1/weeks/169/payrolls?method=post&remote=true" for 200.94.45.34 at 2017-05-27 16:51:29 +0000
Cannot render console from 200.94.45.34! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PayrollsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"lQpIAAD45MF7BNPedoTVJ0rxRwSHSlDFD4RHNrPJT44yzYEeGpwWiFuajIZNUBnceRsstrULDbTxyZdiLOA3bA==", "
  payroll"=>[{"id"=>"258", "week_id"=>"169", "department_id"=>"2", "employee_id"=>"1", "employee_wd_attributes"=>{"employee_wd_id"=>"222", "jueves"=>"1",
  "viernes"=>"4", "sabado"=>"1", "domingo"=>"2", "lunes"=>"2", "martes"=>"2", "miercoles"=>"2"}, "dias_trabajados"=>"1.0", "dias_vacaciones"=>"1", "faltas"=>"1",
  "total_sueldo_bruto"=>"169.0", "employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
  {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
  {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
  {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}, "total_percepciones"=>"", "total_retenciones"=>"", "sueldo_fiscal"=>"169.0", "total_sueldo_neto"=>"169.0"
  , "id"=>"259", "week_id"=>"169", "department_id"=>"2", "employee_id"=>"3", "employee_wd_attributes"=>{"employee_wd_id"=>"223", "jueves"=>"1", "viernes"=>"1", "sabado"=>"1", "domingo"=>"1", "lunes"=>"1",
  "martes"=>"1", "miercoles"=>"11"}, "dias_trabajados"=>"1", "dias_vacaciones"=>"1", "faltas"=>"1", "total_sueldo_bruto"=>"11"}, {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, 
  {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
  {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
  {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, {"employee_wad_attributes"=>{"importe_ads"=>"0"}, 
  "total_percepciones"=>"200.0", "total_retenciones"=>"0.0", "sueldo_fiscal"=>"0.0", "total_sueldo_neto"=>"0.0"}], "commit"=>"guardar", "method"=>"post", "remote"=>"true", "company_id"=>"1", "week_id"=>"169"}
Unpermitted parameter: employee_wd_id
Unpermitted parameter: employee_wd_id
Unpermitted parameters: utf8, authenticity_token, commit, method, remote, company_id, week_id
    
asked by Daniel Romero 27.05.2017 в 19:01
source

1 answer

3
  

Unpermitted parameters: employee_wd_id, employee_wd_attributes

Your method payroll_params has an error in the key employee_wd_attributes , the first attribute says id instead of :id ; to correct it change id by :id :

def payroll_params
  params.permit( 
    payroll: [
      # ... otros parámetros
      employee_wd_attributes: [
        :id, :jueves, :viernes, :sabado, :domingo, :lunes, :martes, :miercoles
      ],
      # ... otros parámetros
    ]
  )
 end

employee_wd_attributes seems not to be generating an error, according to the log you show.

  

... for example if there are 2 rows when the each of the update method   Enter the parameters for the second time, they go null.

The behavior is correct since in the first cycle of payroll_params[:payroll].each you take the first Hash of the% fix% co_of%, which yes has payroll ; Here I show you that first id :

{
  "id"=>"258", 
  "week_id"=>"169", 
  "department_id"=>"2", 
  "employee_id"=>"1", 
  "employee_wd_attributes"=>{
    "employee_wd_id"=>"222", 
    "jueves"=>"1",
    "viernes"=>"4",
    "sabado"=>"1",
    "domingo"=>"2",
    "lunes"=>"2",
    "martes"=>"2", 
    "miercoles"=>"2"
  }, 
  "dias_trabajados"=>"1.0", 
  "dias_vacaciones"=>"1", 
  "faltas"=>"1",
  "total_sueldo_bruto"=>"169.0", 
  "employee_wad_attributes"=>{"importe_ads"=>"0"}
} 

Therefore, when in your method Hash you execute update , if you get results.

So far so good, but look at the second element of the% @payroll = Payroll.find(upd_params[:id]) fix:

{"employee_wad_attributes"=>{"importe_ads"=>"0"}}

As you may notice, no has payroll , therefore, when executing id you get in @payroll = Payroll.find(upd_params[:id]) with id ; in fact the next 11 elements of the arrangement are the same.

I do not know what is the use of these elements, so check if it is correct that they are being sent and, if so, that they are correctly nested in the way you send them.

Here I leave the parameters with a more friendly format, which will allow you to review more clearly each item you receive in NULL :

{
  "utf8"=>"✓", 
  "authenticity_token"=>"lQpIAAD45MF7BNPedoTVJ0rxRwSHSlDFD4RHNrPJT44yzYEeGpwWiFuajIZNUBnceRsstrULDbTxyZdiLOA3bA==", 
  "payroll"=>
  [
    {
      "id"=>"258", 
      "week_id"=>"169", 
      "department_id"=>"2", 
      "employee_id"=>"1", 
      "employee_wd_attributes"=>{
        "employee_wd_id"=>"222", 
        "jueves"=>"1",
        "viernes"=>"4",
        "sabado"=>"1",
        "domingo"=>"2",
        "lunes"=>"2",
        "martes"=>"2", 
        "miercoles"=>"2"
      }, 
      "dias_trabajados"=>"1.0", 
      "dias_vacaciones"=>"1", 
      "faltas"=>"1",
      "total_sueldo_bruto"=>"169.0", 
      "employee_wad_attributes"=>{"importe_ads"=>"0"}
    }, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {
      "employee_wad_attributes"=>{"importe_ads"=>"0"}, 
      "total_percepciones"=>"", 
      "total_retenciones"=>"", 
      "sueldo_fiscal"=>"169.0", 
      "total_sueldo_neto"=>"169.0", 
      "id"=>"259", 
      "week_id"=>"169", 
      "department_id"=>"2", 
      "employee_id"=>"3", 
      "employee_wd_attributes"=>{
        "employee_wd_id"=>"223", 
        "jueves"=>"1", 
        "viernes"=>"1", 
        "sabado"=>"1", 
        "domingo"=>"1", 
        "lunes"=>"1",
        "martes"=>"1", 
        "miercoles"=>"11"
      }, 
      "dias_trabajados"=>"1", 
      "dias_vacaciones"=>"1", 
      "faltas"=>"1", 
      "total_sueldo_bruto"=>"11"
    }, 
    {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"1"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}},
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {"employee_wad_attributes"=>{"importe_ads"=>"0"}}, 
    {
      "employee_wad_attributes"=>{"importe_ads"=>"0"}, 
      "total_percepciones"=>"200.0", 
      "total_retenciones"=>"0.0", 
      "sueldo_fiscal"=>"0.0", 
      "total_sueldo_neto"=>"0.0"
    }
  ], 
  "commit"=>"guardar", 
  "method"=>"post", 
  "remote"=>"true", 
  "company_id"=>"1", 
  "week_id"=>"169"
}
    
answered by 27.05.2017 / 21:30
source