前提・実現したいこと
railsで簡易ブログを作っている際、ルーティングのエラーが発生しました。
投稿を削除しようという部分にも関わらず、ルーティングではdeleteではなくgetメソッドが必要なようで、訳がわからず行き詰まりました。
初心者で申し訳ございませんが、正しくdeleteメソッドで削除を実行できるよう解決策をご教示いただけるとありがたいです。
何卒よろしくお願い致します。
発生している問題・エラーメッセージ
No route matches [GET] "/tweets/5" (5は削除しようとしている投稿に割り当てられたidです)
該当のソースコード
tweets_controllerの開示 ↓ class TweetsController < ApplicationController before_action :move_to_index, except: :index def index @tweets = Tweet.order("created_at DESC").page(params[:page]).per(5) end def new end def create Tweet.create(text:tweet_params[:text], user_id:current_user.id) end def destroy tweet = Tweet.find(params[:id]) tweet.destroy if tweet.user_id == current_user.id end private def tweet_params params.permit(:text) end def move_to_index redirect_to action: :index unless user_signed_in? end end config/routes.rbの開示 ↓ Rails.application.routes.draw do devise_for :users root 'tweets#index' get 'tweets' => 'tweets#index' get 'tweets/new' => 'tweets#new' post 'tweets' => 'tweets#create' delete 'tweets/:id' => 'tweets#destroy' get 'users/:id' => 'users#show' end rails routesの結果は ↓ Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / tweets#index tweets GET /tweets(.:format) tweets#index tweets_new GET /tweets/new(.:format) tweets#new POST /tweets(.:format) tweets#create DELETE /tweets/:id(.:format) tweets#destroy GET /users/:id(.:format) users#show rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create ルーティングでは delete 'tweets/:id' => 'tweets#destroy' 、 tweets_controllerでは、 def destroy tweet = Tweet.find(params[:id]) tweet.destroy if tweet.user_id == current_user.id end ビューファイルでは <%= link_to '削除', "/tweets/#{tweet.id}", method: :delete %> と記述しているのですが、上記のエラーメッセージが出ました。
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ruby 2.5.1
Rails 5.2.4.3
Ruby on Rails タグをつけた方が良いような。
app/assets/javascripts/application.js を開示できますか?
# jquery が悪さしているパターンかもしれません。
ありがとうございます。app/assets/javascripts/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 .
rails routes
の実行結果を質問に追記できますか?
tweets に DELETE がなくて、tweets_new に DELETE ができているのが気になりますね。
tweets_controller の全開示と config/routes.rb の開示は可能ですか?
可能であれば、質問に追記してください。できれば、それぞれ別のソースと分かるようにしてください。
/tweets/:idにdeleteがあるんですが、なぜか機能しないんです。
承知しました、追記してみます。
get 'tweets' => 'tweets#index'
get 'tweets/new' => 'tweets#new'
post 'tweets' => 'tweets#create'
delete 'tweets/:id' => 'tweets#destroy'
を
resources :tweets, :except => :show
に整理したら、どうなりますか?
すみません、resources にまとめて変更しましたが、同じエラーが出ます。
お疲れ様です。layoutファイルに元々記載されていたコードを削除してしまったことで、エラーが出ていることがわかりました。
削除していた部分を再度記入すると、ちゃんとdeleteメソッドで削除ができました。
お手数おかけしてしまい、申し訳ございませんでした。
解決してよかったですね。
逆に混乱させてしまいましたかね。
解決手段をまとめて、解決済みにしていただければと思います。
いえいえ、ありがとうございました。
理解を深めるにはもっと学習期間が必要ですね。
回答2件
あなたの回答
tips
プレビュー