2016-09-21 45 views
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 

這是我的書籍控制器,我在展示文件中的「刪除」如下所示。銷燬功能在RoR中不起作用

<%= link_to 'Delete', book, 
    data: { confirm: 'Are you sure?' }, method: :delete %> 

我收到一個鏈接,但它不工作。我的代碼有什麼問題。提前致謝。

+1

你的意思是「不工作」是什麼意思? 'BooksController#destroy'沒有被調用嗎? 'set_book'失敗了嗎? '@ book.destroy'是否失敗? JavaScript控制檯中的任何錯誤?服務器日誌中的任何內容? –

+0

如果在那裏正確指定了所有內容,則需要調試'set_book'和'authenticate_user!'過濾器。 –

回答

0

確保您將//= require jquery_ujs設置爲 assets/javascripts/application.js

+0

我已經添加了.. –