実現したいこと
家計簿アプリを作成しており、登録したデータを削除する機能を追加したい。
発生している問題・分からないこと
データが削除されない。
エラーメッセージ
error
1エラーメッセージはありません。
該当のソースコード
Rails.application.routes.draw do get "/books", to: "books#index" post "/books", to: "books#create" get "/books/new", to: "books#new", as: "new_book" get "/books/:id/edit", to: "books#edit", as: "edit_book" get "/books/:id", to: "books#show", as: "book" patch "/books/:id", to: "books#update" delete "/books/:id", to: "books#destroy" #該当の処理 end
class BooksController < ApplicationController def index @books = Book.all end def show @book = Book.find(params[:id]) end def new @book = Book.new end def create book_params = params.require(:book).permit(:year, :month, :inout, :category, :amount) @book = Book.new(book_params) if @book.save redirect_to books_path else render :new end end def edit @book = Book.find(params[:id]) end def update @book = Book.find(params[:id]) book_params = params.require(:book).permit(:year, :month, :inout, :category, :amount) if @book.update(book_params) redirect_to books_path else render :edit end end #以下、該当の処理 def destroy @book = Book.find(params[:id]) @book.destroy redirect_to books_path end end
<h1>家計簿</h1> <%= link_to "+新規", new_book_path, class: "btn btn-success" %> <table class="table table-striped"> <tr> <th>年月</th> <th>区分</th> <th>科目</th> <th>金額</th> <th>リンク</th> </tr> <% @books.each do |book| %> <tr> <td><%= book.year %>年<%= book.month %>月</td> <td><%= book.inout %></td> <td><%= book.category %></td> <td><%= book.amount %>万円</td> <td> <%= link_to "詳細", book_path(book), class: "btn btn-info" %> <%= link_to "編集", edit_book_path(book), class: "btn btn-warning" %> <%= link_to "削除", book_path(book), method: "delete", data: {confirm: '本当に削除しますか?'}, class: "btn btn-danger" %> #該当の箇所 </td> </tr> <% end %> </table>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
routes.rbのdeleteの記載を一番上に持っていきましたが、結果は変わりませんでした。
補足
Rails 7.1.3.2
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
開発環境:AWS Cloud9
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/03/12 06:34