現在、掲示板サービスみたいなものをつくっていて、削除機能をつけるために
config/routes.rb
1 Rails.application.routes.draw do 2 get 'topics/index' 3 get 'topics/show/:id' => 'topics#show', as: :topics_show 4 post 'topics/create' => 'topics#create' 5 delete 'topics/delete/:id' => 'topics#delete', as: :topics_delete 6 # For details on the DSL available within this file, see https://guides.r ubyonrails.org/routing.html 7 end
app/controllers/tipic_controller.rb
1 class TopicsController < ApplicationController 2 def index 3 @topics = Topic.all 4 @newtopic = Topic.new 5 end 6 7 def show 8 end 9 10 def create 11 @topic = Topic.new(params[:topic].permit(:title)) 12 @topic.save 13 redirect_to topics_index_path 14 end 15 16 def delete 17 @topic = Topic.find(params[:id]) 18 @topic.destroy 19 redirect_to topics_index_path 20 end 21 end
views/index.html.erb
1<h1>this is topic page</h1> 2 <%= @topics.each do |topic| %> 3 <li><%= link_to topic.title, topics_show_path(topic.id) %></li> 4 5 <li><%= link_to '[Delete]', topics_delete_path(topic.id) %></li> 6 <% end %> 7 8 <h1>create a new topic</h1> 9 <%= form_for @newtopic, :url => {:action => 'create'} do |f| %> 10 <%= f.text_field :title %> 11 <%= f.submit %> 12 <% end %>
上記のようにroutes.rb,contorller,viewsを記述しましたが、indexの削除ボタンを押すと以下のようなエラーになってしまいます。
chrome
1Routing Error 2No route matches [GET] "/topics/delete/6"
そこでやったこととしては
・rake/routesに書かれているroutingとroutes.rbのdelete部が合っているか確認
・showのroutingがちゃんとidごとに振り分けられているか確認(showはidごとのページにアクセスできます。)
・routingをdeleteではなくgetにしてエラーが出るか確認。(getにするとdeleteできましたが、"[]"だけ何故かずっと表示されています)
どうすればroutingが通りdeleteできるようになりますでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/23 13:18