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:

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...

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Elton Santos
  • 571
  • 6
  • 32
  • Are you saving the values as array like the following in your code value_solve: params[:authorization][:value_solve] – Bharat soni Apr 18 '16 at 12:54
  • And? I don't undestand = l – Elton Santos Apr 18 '16 at 12:56
  • Try Authorization.where(id: params[:authorization]['contract_ids']).update_all(value_solve: params[:authorization]['value_solve'], situation: 2) – Chakreshwar Sharma Apr 18 '16 at 12:56
  • @ChakreshwarSharma don't update my table... Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"11111", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["787888", "", ""]}, "commit"=>"Reserve"} SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '--- - ''787888'' - '''' - '''' ', "situation" = 2 WHERE "authorizations"."id" = 11 – Elton Santos Apr 18 '16 at 13:08
  • Line 37 was this: Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2) – Elton Santos Apr 18 '16 at 13:19

4 Answers4

0

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.

margo
  • 2,927
  • 1
  • 14
  • 31
  • Nothing man =/ don't update my table authorization. Processing by RefinancingsController#new as HTML Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"122212", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["34343", "", ""]}, "commit"=>"Reserve"} SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '--- - ''34343'' - '''' - '''' ', "situation" = 2 WHERE "authorizations"."id" = 11 – Elton Santos Apr 18 '16 at 13:13
  • 2
    params are HashWithIndifferentAccess objects, so `params[:authorization]` and `params['authorization']` both work. – SteveTurczyn Apr 18 '16 at 13:30
0
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

Vrushali Pawar
  • 3,753
  • 1
  • 13
  • 22
  • nothing... Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"111111", "authorization"=>{"value_solve"=>["", "3444", "55645"], "contract_ids"=>["33", "22"]}, "commit"=>"Reserve"} SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '--- - '''' - ''3444'' - ''55645'' ', "situation" = 2 WHERE "authorizations"."id" IN (33, 22 – Elton Santos Apr 18 '16 at 13:30
  • there is a change in condition please try this – Vrushali Pawar Apr 18 '16 at 13:37
  • So, I tried, but nothing change... but I guess that know what is the error. Look: – Elton Santos Apr 18 '16 at 13:41
  • Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"222222", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["89888", "", ""]}, "commit"=>"Reserve"} SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '--- - ''89888'' - '''' - '''' ', "situation" = 2 WHERE "authorizations"."id" = 11 ---- Where you see authorizations.id = 11 , its wrong, the corrects, this case, is id 1 – Elton Santos Apr 18 '16 at 13:42
  • Please help me @ROR Developer , its been so complicated = ( – Elton Santos Apr 18 '16 at 13:52
  • @RORDeveloper A code dump without explanation is a sub-optimal answer. – Dave Newton Apr 18 '16 at 13:58
0

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>&nbsp;</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

Elton Santos
  • 571
  • 6
  • 32
  • With the information you've given, no one will be able to figure out why the wrong value is passed. Where are the params coming from? a form? – margo Apr 18 '16 at 18:53
  • Something in that form is passing in 11. View the source code of that form in a browser and see what is getting passed in. Should this line <%= check_box_tag 'authorization[contract_ids][]', authorization.contract_number %> be <%= check_box_tag 'authorization[contract_ids][]', authorization.contract_id %> What is contract_number? – margo Apr 18 '16 at 19:17
  • contract_number is 11. Is checking correctly. The problem is on update. Look: UPDATE "authorizations" SET "value_solve" = '--- - ''777777'' - '''' - '''' ', "situation" = 2 WHERE "authorizations"."id" = 11 – Elton Santos Apr 18 '16 at 19:22
  • My authorization id is 1, contract is 11, I think that the error is here – Elton Santos Apr 18 '16 at 19:23
  • Exactly! You're using the contract id in your where clause. Authorization.where(id: params[:authorization][:contract_ids]). params[:authorization][:contract_ids] == 11. Nowhere in the parameters are you passing in authorization id? How do you expect to get that? – margo Apr 18 '16 at 19:27
  • expect get with params[:authorization], not yes? – Elton Santos Apr 18 '16 at 19:28
  • @margo oh man, I answer, I don't know what I do, serious, I did everything I could :( – Elton Santos Apr 18 '16 at 19:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109474/discussion-between-margo-and-elton-santos). – margo Apr 18 '16 at 19:40
0

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

Elton Santos
  • 571
  • 6
  • 32