Home>

I am making a simple book management app with Ruby on Rails 6.0.0.
It has a structure like category>book, and the routing is nested.
When I tried to implement the book delete feature, I got an error in the index view.
Thanks for your reply.

Error statement (An error appears in the part surrounded by *)
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 
13 **<% link_to "Delete this book", category_book_path (book.id), method:: delete%>**
14 
15 
16

           
           <% = l book.created_at%>
           
         
         
             
             <% = book.content%>
             
           <% = image_tag book.image.variant (resize: '250x600'), class:'book-image' if book.image.attached?%>
             
              **<% link_to "Delete this book", category_book_path (book.id), method:: delete%>**
             
         
 
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 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
tag
  • Answer # 1

    Looking at routes,category_book_pathIs/ categories /: category_id/books /: idIt has become.
    In other words, you need two arguments,: category_id and: id.
    Currently it looks like this and there is only one.

    category_book_path (book.id)

    How about changing to two arguments as shown below?

    category_book_path (@ category.id, book.id)