Я создаю цифровую библиотеку, и сейчас я пытаюсь добавить soft_delete в приложение, но у меня возникают проблемы с ошибкой, показывающей
ActionController :: ParameterMissing в BooksController # update
параметр отсутствует или значение пусто: book
Действие метода soft_delete заключается в обновлении его таблицы в базе данных со значения по умолчанию false до true. Я проверил свой код, но не могу найти, откуда возникла проблема.
Книжная модель
class Book < ApplicationRecord
#add a model scope to fetch only non-deleted records
scope :not_deleted, -> { where(soft_deleted: false) }
scope :deleted, -> { where(soft_deleted: true) }
#create the soft delete method
def soft_delete
update(soft_deleted: true)
soft_deleted
end
# make an undelete method
def undelete
update(soft_deleted: false)
end
end
Контроллер книг (усеченный)
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :soft_delete, :destroy]
def index
@books = Book.not_deleted
end
...
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
format.json { head :no_content }
end
end
def soft_delete
respond_to do |format|
@book.soft_delete(book_params)
format.html { redirect_to books_url, notice: 'Book was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def book_params
params.require(:book).permit(:name, :author, :description, :soft_deleted)
end
end
Маршрут
Rails.application.routes.draw do
resources :books
put '/books/:id' => 'books#soft_delete'
end
Индекс книг
<h1>Books</h1>
<tbody>
<% @books.each do |book| %>
<tr>
<td><%= book.name %></td>
<td><%= book.author %></td>
<td><%= book.description %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Soft Delete', book, method: :put, data: { confirm: 'Are you sure?' } %></td>
soft_delete
</tr>
<% end %>
</tbody>