質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

362閲覧

railsにて削除機能の実装が出来ない

yu-tako

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2023/04/11 06:33

編集2023/04/11 06:38

解決したいこと

削除機能の実装

Ruby on RailsでQiitaのようなWebアプリをつくっています。
削除機能の実装をしましたが、詳細ページに飛んでしまいます。
原因が分からず立ち止まっています。
初歩的なところではあると思いますが、お力を貸して頂けると嬉しいです。

該当するソースコード

_tweet.html.erb

1<div class="content_post" style="background-image: url(<%= tweet.image %>);"> 2 <div class="more"> 3 <span><%= image_tag 'arrow_top.png' %></span> 4 <ul class="more_list"> 5 <li> 6 <%= link_to '詳細', tweet_path(tweet.id), method: :get %> 7 </li> 8 <% if user_signed_in? && current_user.id == tweet.user_id %> 9 <li> 10 <%= link_to '編集', edit_tweet_path(tweet.id), method: :get %> 11 </li> 12 <li> 13 <%= link_to '削除', tweet_path(tweet.id), method: :delete %> 14 </li> 15 <% end %> 16 </ul> 17 </div> 18 <p><%= tweet.tweet %></p> 19 <span class="name"> 20 <a href="/users/<%= tweet.user.id %>"> 21 <span>投稿者</span><%= tweet.user.nickname %> 22 </a> 23 </span> 24</div>

tweets_controller

1class TweetsController < ApplicationController 2 before_action :set_tweet, only: [:edit, :show] 3 before_action :move_to_index, except: [:index, :show] 4 5 def index 6 @tweets = Tweet.includes(:user).order("created_at DESC") 7 end 8 9 def new 10 @tweet = Tweet.new 11 end 12 13 def create 14 Tweet.create(tweet_params) 15 end 16 17 def destroy 18 tweet = Tweet.find(params[:id]) 19 tweet.destroy 20 end 21 22 def edit 23 end 24 25 def update 26 tweet = Tweet.find(params[:id]) 27 tweet.update(tweet_params) 28 end 29 30 def show 31 end 32 33 private 34 35 def tweet_params 36 params.require(:tweet).permit(:tweet, :content, :image).merge(user_id: current_user.id) 37 end 38 39 def set_tweet 40 @tweet = Tweet.find(params[:id]) 41 end 42 43 def move_to_index 44 unless user_signed_in? 45 redirect_to action: :index 46 end 47 end 48end

application.js

1 2// This file is automatically compiled by Webpack, along with any other files 3// present in this directory. You're encouraged to place your actual application logic in 4// a relevant structure within app/javascript and only use these pack files to reference 5// that code so it'll be compiled. 6 7require("@rails/ujs").start() 8require("turbolinks").start() 9require("@rails/activestorage").start() 10require("channels") 11//= require rails-ujs 12 13// Uncomment to copy all static images under ../images to the output folder and reference 14// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) 15// or the `imagePath` JavaScript helper below. 16// 17// const images = require.context('../images', true) 18// const imagePath = (name) => images(name, true) 19

package.json

1{ 2 "dependencies": { 3 "@rails/webpacker": "4.3.0" 4 }, 5 "devDependencies": { 6 "webpack-dev-server": "^4.13.2" 7 } 8} 9

postcss.config.js

1module.exports = { 2 plugins: [ 3 require('postcss-import'), 4 require('postcss-flexbugs-fixes'), 5 require('postcss-preset-env')({ 6 autoprefixer: { 7 flexbox: 'no-2009' 8 }, 9 stage: 3 10 }) 11 ] 12}

gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.6.5' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 7gem 'rails', '~> 6.0.0' 8# Use mysql as the database for Active Record 9gem 'mysql2', '0.5.3' 10# Use Puma as the app server 11gem 'puma', '~> 3.11' 12# Use SCSS for stylesheets 13gem 'sass-rails', '~> 5' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 4.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', '>= 1.4.2', require: false 30 31group :development, :test do 32 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 33 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 34end 35 36group :development do 37 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 38 gem 'web-console', '>= 3.3.0' 39 gem 'listen', '>= 3.0.5', '< 3.2' 40 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 41 gem 'spring' 42 gem 'spring-watcher-listen', '~> 2.0.0' 43end 44 45group :test do 46 # Adds support for Capybara system testing and selenium driver 47 gem 'capybara', '>= 2.15' 48 gem 'selenium-webdriver' 49 # Easy installation and use of web drivers to run system tests with browsers 50 gem 'webdrivers' 51end 52 53# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 54gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 55gem 'pry-rails' 56gem 'net-http' 57gem 'devise' 58gem 'jquery-rails'

削除ボタンを押した後のターミナル画像

https://gyazo.com/24b48c7918bdb978bdb53d88d1a65d7a

仮説

・パスの設定がうまく出来ていないのではないか
・必要なファイルが抜けているのではないか
・Controllerアクション記述がおかしいのではないか

自分で試したこと

・yarnのインストール、update
・gemfileにgem 'jquery-rails'を追加しbundle install
・application.jsに//= require rails-ujsを追記
・rails routesで再度deleteパスを確認

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問