在Rails 6中删除多个复选框

时间:2019-09-20 13:25:03

标签: ruby-on-rails ruby-on-rails-6

我正在rails 6应用上创建多个删除功能。

到目前为止,这是我已经完成的步骤。

  1. 创建了destroy_multiple方法
def destroy_multiple
  Page.destroy(params[:page_ids])
  redirect_to pages_path
end 
  1. 编辑了我的config->routes.rb
resources :pages do 
  collection do 
    delete 'destroy_multiple'
  end 
end
  1. 已安装gem 'select_all-rails'

  2. 在我的app->javascript->application.js中,我包括了选择所有rails js所需的代码:

require("@rails/ujs").start()
Require ("turbolinks"). start ()
require("@rails/activestorage").start()
require("channels")

require("select_all.js")

$(function() {
  $("#selectAll").select_all();
});

import 'bootstrap'
import './stylesheets/application.scss'
  1. 最后,在我看来,我包括以下代码:
    <section>
            <div class="container-fluid">
              <!-- Page Header-->
              <header> 
                <h1 class="h3 display">Pages</h1>
              </header>
              <div class="row">
                <div class="col-lg-12">
                  <div class="card">
                    <div class="card-header">
                      <h4>List of Pages</h4>
                    </div>
                    <div class="card-body">
                      <div class="table-responsive">

    <%= form_tag destroy_multiple_pages_path, method: :delete do %>

                        <table class="table">
                          <thead>
                            <tr>
                             <th data-sort-ignore="true"> <input type="checkbox" id="selectAll"/></th>
                              <th>Id</th>
                              <th>Title</th>
                              <th>Summary</th>
                              <th>Date Created</th>
                              <th colspan="3">Action</th>
                            </tr>
                          </thead>
                          <tbody>
                          <% @pages.each do |page| %>
                          <tr>
                            <td><%= check_box_tag "pages_ids[]", page.id, false, class: 'selectable' %> </td>
                              <td><input type="checkbox" value="false"> </td>
                              <td scope="row"><%= page.id %></td>
                              <td><%= page.title %></td>
                              <td><%= page.body.truncate(60) %></td>
                              <td> <%= page.created_at.strftime("%B %d, %Y") %> </td>
                            <td><%= link_to "Show", page_path(page)%></td>
                             <td><%= link_to "Edit", edit_page_path(page)%></td>
                              <td><%= link_to "Delete", page_path(page), method: :delete, data: {  confirm: "Are you sure?" }%></td>
                            </tr>
                            <% end %>
                          </tbody>
                        </table>

    <%= submit_tag "Delete Selected", class: 'btn btn-primary btn-xs' %>

    <% end %>

                      </div>
                    </div>
                  </div>
                </div>

运行此命令时,它会正确显示我的复选框。但是,当我单击顶部的复选框时,并没有全部选中,单击删除按钮后出现此错误:

Couldn't find Page without an ID

知道我在做什么错吗?

0 个答案:

没有答案