没有将 Symbol 隐式转换为 Integer。一个错误对我来说仍然没有解决方案

No implicit conversion of Symbol into Integer. A error still no solution for me

我有一个带有复选框和输入文本的视图。提交后,我在输入时选中了一个复选框和数字值。但是有两个问题...参数错误并且不保存我的 table 上的值,这是我的代码:

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

我的控制台是:

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'

这是第一个错误:

no implicit conversion of Symbol into Integer

另一个错误是不显示参数情况...

Rails 将参数散列的键作为字符串而不是符号传递,正如您在提供的堆栈跟踪中看到的那样。

Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"11111111111", "authorization"=>{"value_solve"=>["", "3444555", ""], "contract_ids"=>["33"]}, "commit"=>"Reserve"}

更改您的代码以使用字符串,例如

if params["authorization"]
  @selected_ids = params["authorization"]["contract_ids"]

等等

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

试试这个

使用此代码:

  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

没有更多的错误,好的,但是不要保存在我的 table 授权中,因为 id 是错误的。看看我的控制台:

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)

"authorizations"."id" = 11,但是我没有id 11授权,本来是id 1。怎么办?请 : (

已更新-----------------------------

这是我的观点index.html.erb。我标记了复选框并在输入 value_solve 上输入了一些值,然后单击 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 %>

表单索引的控制器很简单而且可以工作,就是这样:

  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

点击 Reserve 后,勾选了复选框和数字我的控制台是这样的,我是怎么说的:

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

答案是:

      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