我有一个带有复选框和输入文本的视图。 提交后,我在输入中选中了一个复选框和数字值。但是有两个问题... params上的错误并且不保存我的表上的值,这是我的代码:
refinancings_controller.rb
def new
if params[:authorization]
@selected_ids = params[:authorization][:contract_ids]
@authorizations = Authorization.where("contract_number in (?)", @selected_ids)
end
@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)
end
end
这是我的控制台:
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)
错误是:
Couldn't find Authorization with 'id'= 0
我的观点是正确的。其他的事情。再融资belongs_to:授权和授权has_many:refinancings 当我检查时,单击复选框的id传递ok和value_solve,但情况不会更改,并且没有任何保存在db
答案 0 :(得分:0)
你应该只使用each而不是each_with_index。现在设置它的方式是,将数组的索引传递给find方法。数组的第一个索引始终为0,这就是您收到该错误的原因。您可以这样设置:
params[:authorization][:contract_ids].each do |id|
答案 1 :(得分:0)
使用此代码:
Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: [:authorization][:value_solve])
显示此错误:
TypeError in RefinancingsController#new
no implicit conversion of Symbol into Integer
答案 2 :(得分:0)
答案是:
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)
end
:d