前提・実現したいこと
railsでレコードの削除機能を作りたく、ネットで調べたやり方を使ってもうまく機能してくれません。
発生している問題・エラーメッセージ
ActionController::RoutingError (No route matches [GET] "/articles/16"):
該当のソースコード
index.html.erb
1<ol> 2 <% @articles.each do |article| %> 3 <li> 4 <%= article.title %><br> 5 <%= article.text %><br> 6 <%= link_to "削除", "/articles/#{article.id}", method: :delete %> 7 </li> 8 <% end %> 9</ol>
Ruby
1#routes.rb 2Rails.application.routes.draw do 3 root "articles#index" 4 5 resources :articles, only: [:index, :new, :create, :destroy] 6end
Ruby
1#articles_controller.rb 2class ArticlesController < ApplicationController 3 protect_from_forgery :except => [:destroy] 4 5 def index 6 @articles = Article.all 7 end 8 9 def new 10 @article = Article.new 11 end 12 13 def create 14 @article = Article.new(article_params) 15 16 if @article.save 17 redirect_to root_path 18 else 19 render :new, status: :unprocessable_entity 20 end 21 end 22 23 def destroy 24 @article = Article.find(params[:id]) 25 @article.destroy 26 27 redirect_to root_path 28 end 29 30 private 31 def article_params 32 params.require(:article).permit(:title, :text) 33 end 34end 35
試したこと
link_toをbutton_toに変更したが変わらなかったです。
https://qiita.com/mami3sansan/items/6a9710b3ffdc937aa5f3
こちらの記事に書かれた内容を試してみたのですがうまくいきませんでした。
補足情報(FW/ツールのバージョンなど)
Mac 12.1
Ruby 3.1.0
Rails 7.0.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/21 14:44