0

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

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

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

4

4 回答 4

0

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

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"]

等等

于 2016-04-18T13:03:01.953 回答
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

于 2016-04-20T13:03:09.403 回答
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

没有更多的错误,好的,但是不要保存在我的表中授权,因为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

单击保留后,选中复选框并数字化我的控制台是这样的,我怎么说:

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

于 2016-04-18T14:22:14.677 回答
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

尝试这个

于 2016-04-18T13:22:47.197 回答