0
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def show
@reviews = Review.where(book_id: @book.id).order("created_at DESC")
if @reviews.blank?
@avg_review = 0
else
@avg_review = @reviews.average(:rating).round(2)
end
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
private
def set_book
@book = Book.find(params[:id])
end
def book_params
params.require(:book).permit(:title, :author, :language, :year, :description, :total_pages, :rating, :image)
end
end
これは私のbookscontrollerで、showファイルの "delete"は以下のとおりです。破壊機能がRoRで機能していません
<%= link_to 'Delete', book,
data: { confirm: 'Are you sure?' }, method: :delete %>
私はリンクを取得していますが、動作しません。私のコードに何が間違っていますか?前もって感謝します。
具体的には「働いていない」とはどういう意味ですか? 'BooksController#destroy'は呼び出されませんか? 'set_book'は失敗しますか? '@ book.destroy'は失敗しますか? JavaScriptコンソールにエラーがありますか?サーバーのログには何もありませんか? –
すべてが正しく割り当てられている場合は、 'set_book'と' authenticate_user! 'フィルタをデバッグする必要があります。 –