Reputation: 581
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:
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
Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)
end
My console this is:
Processing by RefinancingsController#new as HTML
Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"11111111111", "authorization"=>{"value_solve"=>["", "3444555", ""], "contract_ids"=>["33"]}, "commit"=>"Reserve"}
Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.3ms)
TypeError (no implicit conversion of Symbol into Integer):
app/controllers/refinancings_controller.rb:37:in `[]'
app/controllers/refinancings_controller.rb:37:in `new'
This is a first error:
no implicit conversion of Symbol into Integer
The other error is don't show params situation...
Upvotes: 0
Views: 1508
Reputation: 581
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)
end
:D
Upvotes: 0
Reputation: 581
With this code:
def new
if params[:authorization].present?
@selected_ids = params[:authorization][:contract_ids]
@authorizations = Authorization.where("contract_number in (?)", @selected_ids)
Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)
end
@employee = Employee.search_cpf(params[:search_employee_by_cpf])
@refinancing = Refinancing.new
Have no more errors, ok, but don't save in my table Authorization, because the id is get wrong. Look my console:
Started GET "/refinancings/new?utf8=%E2%9C%93&search_employee_by_cpf=02849112321&authorization%5Bcontract_ids%5D%5B%5D=11&authorization%5Bvalue_solve%5D%5B%5D=89888&authorization%5Bvalue_solve%5D%5B%5D=&authorization%5Bvalue_solve%5D%5B%5D=&commit=Reserve" for 127.0.0.1 at 2016-04-18 10:40:08 -0300
Processing by RefinancingsController#new as HTML
Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"1111111111", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["89888", "", ""]}, "commit"=>"Reserve"}
SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '---
- ''89888''
- ''''
- ''''
', "situation" = 2 WHERE "authorizations"."id" = 11
Employee Load (0.2ms) SELECT "employees".* FROM "employees" INNER JOIN "people" ON "people"."id" = "employees"."person_id" WHERE (people.cpf LIKE '%02849112321%') ORDER BY "employees"."id" ASC LIMIT 1
Person Load (0.1ms) SELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT 1 [["id", 1]]
Authorization Load (0.2ms) SELECT "authorizations".* FROM "authorizations" WHERE (contract_number in ('11'))
Rendered refinancings/new.html.erb within layouts/application (70.5ms)
Completed 200 OK in 119ms (Views: 85.2ms | ActiveRecord: 1.4ms)
The "authorizations"."id" = 11, but I don't have id 11 for authorizations, was to be id 1. What I do? Please : (
UPDATED --------------------------------
This is my view index.html.erb. I marked the checkboxes and digit some value on input value_solve and click in Reserve
<% if params[:search_employee_by_cpf].present? %>
<h4><b>Results</b></h4>
<%= form_tag(new_refinancing_path, name: 'form', method: :get) do %>
<%= hidden_field_tag 'search_employee_by_cpf', params[:search_employee_by_cpf] %>
<% @employees.each do |employee| %>
<table class="table table-condensed">
<tr>
<th>Name</th>
<td><%= employee.person.name %></td>
</tr>
<tr>
<th>Register</th>
<td><%= employee.register %></td>
</tr>
<tr>
<th>CPF</th>
<td><%= employee.person.cpf %></td>
</tr>
</table>
<hr />
<table class="table table-condensed table-bordered table-hover">
<thead>
<th> </th>
<th>Contract number</th>
<th>Total Value</th>
<th>Value to Solve</th>
</thead>
<tbody>
<% employee.authorizations.each do |authorization| %>
<tr>
<td><%= check_box_tag 'authorization[contract_ids][]', authorization.contract_number %></td>
<td><%= authorization.contract_number %></td>
<td><%= authorization.total_value %></td>
<td>
<input class="string required" placeholder="Digit a value" type="text" name="authorization[value_solve][]" id="authorization_value_solve">
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<%= submit_tag "Reserve %>
<% end %>
<% end %>
The controller for form index is simple and work, just this:
def index
if params[:search_employee_by_cpf].present?
@employees = Employee.search_cpf(params[:search_employee_by_cpf]).includes(:authorizations)
Authorization.search_employee_by_cpf(params[:search_employee_by_cpf]).all
else
@authorizations = []
end
end
After click in Reserve, with the checkboxes checked and value digited my console is this, how I said:
Started GET "/refinancings/new?utf8=%E2%9C%93&search_employee_by_cpf=02849112321&authorization%5Bcontract_ids%5D%5B%5D=11&authorization%5Bvalue_solve%5D%5B%5D=777777&authorization%5Bvalue_solve%5D%5B%5D=&authorization%5Bvalue_solve%5D%5B%5D=&commit=Reserve" for 127.0.0.1 at 2016-04-18 16:08:37 -0300
Processing by RefinancingsController#new as HTML
Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"02849112321", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["777777", "", ""]}, "commit"=>"Reserve"}
SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '---
- ''777777''
- ''''
- ''''
', "situation" = 2 WHERE "authorizations"."id" = 11
sdsds
Upvotes: 0
Reputation: 3803
def new
if params[:authorization].present?
@selected_ids = params[:authorization][:contract_ids]
@authorizations = Authorization.where("contract_number in (?)", @selected_ids)
Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)
end
@employee = Employee.search_cpf(params[:search_employee_by_cpf])
@refinancing = Refinancing.new
end
Try this
Upvotes: 0
Reputation: 2927
Rails passes the keys of the params hash as strings not symbols, as you can see in the stack trace that you've provided.
Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"11111111111", "authorization"=>{"value_solve"=>["", "3444555", ""], "contract_ids"=>["33"]}, "commit"=>"Reserve"}
Change your code to use strings e.g.
if params["authorization"]
@selected_ids = params["authorization"]["contract_ids"]
etc.
Upvotes: 0