Ruby on Rails6.0.0で簡単な本の管理アプリを作っています。
category>bookのような構造になっていて、ルーティングはネストしてあります。
本の削除機能を実装しようとしたところ、indexのビューでエラーが発生しました。
ご回答よろしくお願いします。
##エラー文(※で囲った部分でエラーが出ています)
No route matches {:action=>"destroy", :category_id=>48, :controller=>"books"}, missing required keys: [:id] Extracted source (around line #13): 11 <%= image_tag book.image.variant(resize: '250x600'), class: 'book-image' if book.image.attached? %> 12 <div class="book-delete-button"> 13 ** <% link_to "この本を削除", category_book_path(book.id), method: :delete %>** 14 </div> 15 </div> 16 </div>
<div class="upper-book"> <div class="book-date"> <%= l book.created_at %> </div> </div> <div class="lower-book"> <div class="book-content"> <%= book.content %> </div> <%= image_tag book.image.variant(resize: '250x600'), class: 'book-image' if book.image.attached? %> <div class="book-delete-button"> ** <% link_to "この本を削除", category_book_path(book.id), method: :delete %>** </div> </div> </div>
def index @categorys = Category.all @book = Book.new @category = Category.find(params[:category_id]) @books = @category.books.includes(:user) end def create @categorys = Category.all @category = Category.find(params[:category_id]) @book = @category.books.new(book_params) if @book.save redirect_to category_books_path(@category) else @books = @category.books.includes(:user) render :index end end def destroy @categorys = Category.all @category = Category.find(params[:category_id]) @book = @category.books.find(book_params) end private def book_params params.require(:book).permit(:content, :image).merge(user_id: current_user.id) end end
POST /users(.:format) devise/registrations#create books_index GET /books/index(.:format) books#index root GET / categories#index edit_user GET /users/:id/edit(.:format) users#edit user PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update category_books GET /categories/:category_id/books(.:format) books#index POST /categories/:category_id/books(.:format) books#create category_book DELETE /categories/:category_id/books/:id(.:format) books#destroy categories POST /categories(.:format) categories#create new_category GET /categories/new(.:format) categories#new category DELETE /categories/:id(.:format) categories#destroy タグ
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/03 03:04