Elton Santos Elton Santos - 1 year ago 48
Ruby Question

Receive params and save in other model and remove error Couldn't find Authorization with 'id'= 0

I have a view with checkboxes and input text.
I checked one checkbox and digit value on input, after submit. But have two problem... A error on params and don't save the values on my table, this is my code:


def new
if params[:authorization]
@selected_ids = params[:authorization][:contract_ids]
@authorizations = Authorization.where("contract_number in (?)", @selected_ids)
@employee = Employee.search_cpf(params[:search_employee_by_cpf])
@refinancing = Refinancing.new
params[:authorization][:contract_ids].each_with_index do |id, index|
Authorization.find(id).update_column(value_solve: params[:authorization][:value_solve], situation: 2)

This is my console:

Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"123456789", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["334545", "", ""]}, "commit"=>"Reserve"}
Completed 500 Internal Server Error in 18ms (ActiveRecord: 0.6ms)

And the error is:

Couldn't find Authorization with 'id'= 0

My view is correct. Other thing. Refinancing belongs_to :authorization and Authorizarion has_many :refinancings
When I checked, the id of checkboxes clicked pass ok and the value_solve too, but the situation don't change and nothing save on db

Answer Source

The answer this is:

      auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?)).each do |contract_number, value_solve|
          Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download