0

チェックボックスと入力テキストを含むビューがあります。送信後、入力時に1つのチェックボックスと数字の値をチェックしました。しかし、2つの問題があります... 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
    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
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 に答える
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 が間違っているため、Authorization テーブルに保存しないでください。私のコンソールを見てください:

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 に値を入力して、[予約] をクリックします。

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

SDDS

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