Rails分页表单不偶尔发布参数

时间:2018-11-08 07:09:25

标签: ruby-on-rails forms post will-paginate

我的一种表格遇到间歇性问题。 我已经尝试实现一个批量销毁,它在大多数情况下都可以使用,但是十分之一却无法使用,在控制台中,我收到了一个空的POST请求,没有参数。

这是我的观点:

<h4>Suggestions </h4>
<%= submit_tag "Delete selected", form: "suggestions-delete-form", class: 'btn btn-danger' %>
        <p class="card-text"> 
          <% if @suggestions %>
            <div class="table-responsive-sm">
              <table class="table-sm", id="suggestion-table">
                <tbody>
                  <%= form_tag collection_batch_destroy_path(@collection), method: :post, id: "suggestions-delete-form" do %>
                    <% @suggestions.each do |s| %>
                      <tr id='suggestion'>
                        <th style="width: 4%"><%= check_box_tag "item_ids[]", s.id, false, class: "form-check .selectable-checkbox" %></th>
                        <td><%if s.search.term.include?("-") %>
                              <%= Hpricot.uxs s.search.term.capitalize %>
                            <% else %>
                              <%= Hpricot.uxs s.search.term.titleize %>
                            <% end %>
                        </td> 
                        <td style="width: 4%"><%= link_to fa_icon('plus-circle'), collection_item_choose_path(@collection, s),{  method: :post, class: "btn btn-primary btn-sm text-right" } %>
                        </td>
                        <td style="width: 4%" id='delete-suggestion'><%= link_to fa_icon('trash'), collection_item_destroy_path(@collection, s),{  method: :post, class: "btn btn-primary btn-sm text-right" } %><br>
                        </td>
                      </tr>
                    <% end %>
                  <% end %>
                </tbody>
              </table>
            </div>
          <% end %>
        </p>

这些是我的路线:

  resources :sellers, only: [:show] do
    resources :collections, shallow: true do
      match 'item/choose', to: 'items#choose', via: 'post'
      match 'item/remove', to: 'items#remove', via: 'post'
      match 'item/destroy', to: 'items#destroy', via: 'post'
      match 'batch_destroy', to: 'items#batch_destroy', via: 'post'
    end
  end

这是我的控制器:

  def batch_destroy
    @collection = Collection.find(params[:collection_id])
    if params[:item_ids] && params[:item_ids].size > 0
      Item.where(id: params[:item_ids]).update_all(suggestion: false)
      flash[:success] = "Suggestions successfully deleted"
    else
      flash[:danger] = "No items were selected"
    end
    redirect_to collection_path(@collection)
  end

这是一个有效的批量销毁示例:

Started POST "/collections/67/batch_destroy" for 127.0.0.1 at 2018-11-07 20:16:19 +0200
Processing by ItemsController#batch_destroy as HTML
  Parameters: {"commit"=>"Delete selected", "utf8"=>"✓", "authenticity_token"=>"zEuCw1VX689LwCBcbNsC9JOd2cSVzcc1LxtkFN+xV3I2ORT6r/5aWM8iaNwo5U2LLFF/1lQDIAHQOoVylb4N3A==", "item_ids"=>["12729", "12730", "12731", "12732", "12733", "12735", "12736"], "collection_id"=>"67"}
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Notification Load (0.5ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."recipient_id" = 1 ORDER BY "notifications"."id" DESC LIMIT $1  [["LIMIT", 3]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Seller Load (0.3ms)  SELECT  "sellers".* FROM "sellers" WHERE "sellers"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Shop Load (0.5ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2  [["id", 8], ["LIMIT", 1]]
  Item Load (0.8ms)  SELECT "items".* FROM "items" WHERE "items"."id" IN (12729, 12730, 12731, 12732, 12733, 12735, 12736)
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.552499"], ["id", 12729]]
   (1.1ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.556805"], ["id", 12732]]
   (2.2ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.562274"], ["id", 12735]]
   (3.2ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.5ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.569024"], ["id", 12736]]
   (1.3ms)  COMMIT
   (0.3ms)  BEGIN
  SQL (1.0ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.573374"], ["id", 12730]]
   (5.1ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.7ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.582742"], ["id", 12731]]
   (1.0ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.7ms)  UPDATE "items" SET "suggestion" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["suggestion", "f"], ["updated_at", "2018-11-07 18:16:19.586827"], ["id", 12733]]
   (2.9ms)  COMMIT
  Collection Load (0.3ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 67], ["LIMIT", 1]]
Redirected to http://localhost:3000/collections/67
Completed 302 Found in 57ms (ActiveRecord: 26.3ms)

在这里,我正在执行相同的操作(选择一些项目,单击“删除选定的内容”,但结果是:什么都不做:

Started POST "/collections/67/batch_destroy" for 127.0.0.1 at 2018-11-08 09:36:02 +0200
Processing by ItemsController#batch_destroy as HTML
  Parameters: {"commit"=>"Delete selected", "collection_id"=>"67"}
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Notification Load (0.7ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."recipient_id" = 1 ORDER BY "notifications"."id" DESC LIMIT $1  [["LIMIT", 3]]
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Seller Load (0.5ms)  SELECT  "sellers".* FROM "sellers" WHERE "sellers"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Shop Load (0.3ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2  [["id", 8], ["LIMIT", 1]]
  Item Load (0.5ms)  SELECT "items".* FROM "items" WHERE "items"."id" IS NULL
  Collection Load (0.2ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 67], ["LIMIT", 1]]
Redirected to http://localhost:3000/collections/67
Completed 302 Found in 13ms (ActiveRecord: 3.2ms)

如您所见,尽管选中了一些复选框,但rails并没有传递相应的ID。我还收到Flash消息:“未选择任何项目”。

在开发和生产中都会发生这种情况。 我非常感谢您可能有任何见解。 TIA!

编辑: 刚刚删除了此表单上的will_paginate分页,现在它就像一种魅力。 有人知道是什么原因造成的吗?

0 个答案:

没有答案