前提・実現したいこと
プログラミング初学者です。
Ruby on Railsでブックマーク管理アプリを作成しています。
今回、ブックマーク一覧画面からチェックボックスで選択したブックマーク(1個〜複数個)をまとめて削除できる機能を実装しました。
発生している問題・エラーメッセージ
1度目は問題なく動くのですが、2回目以降はサブミットを押しても全く動きませんでした。
パターンを把握しましたが
・1度目はしっかり削除される
・2回目は動かない
・別のアクションを行った後も動かない
・リロードをすれば動く
エラーも出ずに、Railsサーバーの動きを見ても動いていない(リクエストがされていない?)状態です。
お力添えを頂ければ幸いです。
該当のソースコード
index.html.erb
<%= form_with model: @contents, url: contents_path, method: :delete do |f| %> <% @contents.each do |content| %> <% @count += 1%> <tr> <td> <%= f.check_box :content_ids, {type: 'checkbox', class: 'checkbox-select', id: "checkbox#{@count}", multiple: true, include_hidden: false}, content.id %> <%= f.label :'', for: "checkbox#{@count}",class: 'select-label' %> </td> <td> <%= link_to content.name, content %> </td> <td> <%= link_to 'URL', content.url %> </td> <%#概要は空白の場合の処理も記載%> <td><%= link_to content.description.blank? ? 'None' : content.description, content %></td> <%#カテゴリーは空白の場合の処理も記載%> <td><%= link_to content.category.blank? ? 'None' : content.category, content %></td> <td><%= link_to content.updated_at.to_s(:datetime_jp), content %></td> </tr> <% end %> <%= f.button contents_path, data_toggle: 'tooltip', data_placement: 'topdo', title: '削除',data: {confirm: "ブックマークを削除します", turbolinks: false},class: 'select_submit' do %> <%= embedded_svg('delete.svg', class: "delete")%> <% end %> <% end %>
contents_controller.rb
class ContentsController < ApplicationController def index @contents = Content.all count = 0 @count = count end def show @content = Content.find(params[:id]) end def edit @content = Content.find(params[:id]) end def new @content = Content.new end def create content = Content.new(content_params) content.save! redirect_to contents_url, notice: "タイトル「#{content.name}」を投稿しました" end def update content = Content.find(params[:id]) content.update!(content_params) redirect_to contents_url, notice: "タイトル「#{content.name}」を更新しました" end def destroy content = Content.find(params[:id]) content.destroy redirect_to contents_url, notice: "タイトル「#{content.name}」を削除しました" end def select_destroy select_content_params.each{|id| content = Content.find(id) content.destroy } redirect_to contents_path, notice: "ブックマークを削除しました" end private def content_params params.require(:content).permit(:name, :url, :description, :category, :private) end def select_content_params ids = params.require(:content).permit(content_ids: []) ids.values[0] end end
routes.rb
resources :contents delete "contents" => "contents#select_destroy"
ターミナル上のサーバーログ
tarted
1Processing by ContentsController#select_destroy as JS 2 Parameters: {"utf8"=>"✓", "authenticity_token"=>"2K/5lDMPFkaUeG9IXkEbHxj5nhxhj8wtyxNSGP2MqrKiZVVB5f9CZ6BJPsUCy1EcMk1XFN73IZ4qis08BF/tcg==", "content"=>{"content_ids"=>["133", "136"]}} 3 Content Load (0.8ms) SELECT "contents".* FROM "contents" WHERE "contents"."id" = $1 LIMIT $2 [["id", 133], ["LIMIT", 1]] 4 ↳ app/controllers/contents_controller.rb:40 5 (0.2ms) BEGIN 6 ↳ app/controllers/contents_controller.rb:41 7 Content Destroy (0.3ms) DELETE FROM "contents" WHERE "contents"."id" = $1 [["id", 133]] 8 ↳ app/controllers/contents_controller.rb:41 9 (1.3ms) COMMIT 10 ↳ app/controllers/contents_controller.rb:41 11 ActiveStorage::Attachment Load (1.0ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 133], ["record_type", "Content"], ["name", "image"], ["LIMIT", 1]] 12 ↳ app/controllers/contents_controller.rb:41 13 Content Load (0.6ms) SELECT "contents".* FROM "contents" WHERE "contents"."id" = $1 LIMIT $2 [["id", 136], ["LIMIT", 1]] 14 ↳ app/controllers/contents_controller.rb:40 15 (0.2ms) BEGIN 16 ↳ app/controllers/contents_controller.rb:41 17 Content Destroy (0.3ms) DELETE FROM "contents" WHERE "contents"."id" = $1 [["id", 136]] 18 ↳ app/controllers/contents_controller.rb:41 19 (1.1ms) COMMIT 20 ↳ app/controllers/contents_controller.rb:41 21 ActiveStorage::Attachment Load (0.3ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 136], ["record_type", "Content"], ["name", "image"], ["LIMIT", 1]] 22 ↳ app/controllers/contents_controller.rb:41 23Redirected to http://localhost:3000/contents 24Completed 200 OK in 23ms (ActiveRecord: 6.1ms) 25 26 27Started GET "/contents" for ::1 at 2020-05-21 12:38:00 +0900 28Processing by ContentsController#index as HTML 29 Rendering contents/index.html.erb within layouts/application 30 Content Load (6.9ms) SELECT "contents".* FROM "contents" 31 ↳ app/views/contents/index.html.erb:59 32 Rendered contents/index.html.erb within layouts/application (29.2ms) 33Completed 200 OK in 119ms (Views: 108.8ms | ActiveRecord: 6.9ms)
application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's // vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require rails-ujs //= require activestorage //= require turbolinks //= require_tree. //= require jquery //= require popper //= require bootstrap-sprockets // 検索バー contents/index document.addEventListener("touchstart", function () { }, true);
Gemfile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.4', '>= 5.2.4.2' # Use postgresql as the database for Active Record gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'bootstrap' gem "jquery-rails" gem 'inline_svg'
試したこと
turbolinksを切ったりもしましたが、挙動は変化がありませんでした。
原因特定が出来ず調べるにも困難しています。
↓ 上記内容で不足でしたらお声がけもしくは下記リンクからソースコードをご覧いただけると幸いです。
Github
回答1件
あなたの回答
tips
プレビュー