2016-10-03 118 views
0

我對Rails相當陌生。我正試圖推出一本書評應用程序投入生產。我鬆散地基於它的在線教程。我有一個按鈕,應該會從我的應用程序刪除一個「書」條目。本書的展示頁面上有一個刪除按鈕。但是,當我單擊以刪除圖書條目時,它會重定向到該圖書展示頁面,而無需執行任何操作。該按鈕在生產中非常完美。「摧毀」不生產 - Rails應用程序

我DID已經預編譯了我的資產。所以我對於發生了什麼感到困惑。

這裏是Heroku的日誌:

2016-10-03T18:28:09.421132+00:00 heroku[router]: at=info method=GET  path="/books/3" host=seaboltapplication.herokuapp.com request_id=9c80f545-4de0-4330-9771-b97f47c49d06 fwd="69.181.73.43" dyno=web.1 connect=1ms service=106ms status=200 bytes=3350 
2016-10-03T18:28:09.295950+00:00 app[web.1]: I, [2016-10-03T18:28:09.295839 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Started GET "/books/3" for 69.181.73.43 at 2016-10-03 18:28:09 +0000 
2016-10-03T18:28:09.297370+00:00 app[web.1]: I, [2016-10-03T18:28:09.297254 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Processing by BooksController#show as HTML 
2016-10-03T18:28:09.297509+00:00 app[web.1]: I, [2016-10-03T18:28:09.297433 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Parameters: {"id"=>"3"} 
2016-10-03T18:28:09.318597+00:00 app[web.1]: D, [2016-10-03T18:28:09.318473 #3] DEBUG -- : [9c80f545-4de0-4330-9771-b97f47c49d06] User Load (6.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] 
2016-10-03T18:28:09.328080+00:00 app[web.1]: D, [2016-10-03T18:28:09.327992 #3] DEBUG -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Book Load (3.2ms) SELECT "books".* FROM "books" WHERE "books"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] 
    2016-10-03T18:28:09.355978+00:00 app[web.1]: D, [2016-10-03T18:28:09.355856 #3] DEBUG -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Review Load (24.8ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."book_id" = $1 [["book_id", 3]] 
2016-10-03T18:28:09.357259+00:00 app[web.1]: I, [2016-10-03T18:28:09.357181 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Rendering books/show.html.erb within layouts/application 
2016-10-03T18:28:09.384356+00:00 app[web.1]: D, [2016-10-03T18:28:09.384194 #3] DEBUG -- : [9c80f545-4de0-4330-9771-b97f47c49d06] (15.7ms) SELECT COUNT(*) FROM "reviews" WHERE "reviews"."book_id" = $1 [["book_id", 3]] 
2016-10-03T18:28:09.385772+00:00 app[web.1]: I, [2016-10-03T18:28:09.385704 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Rendered collection of templates [0 times] (0.0ms) 
2016-10-03T18:28:09.387771+00:00 app[web.1]: I, [2016-10-03T18:28:09.387705 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Rendered books/show.html.erb within layouts/application (28.5ms) 
2016-10-03T18:28:09.395073+00:00 app[web.1]: I, [2016-10-03T18:28:09.394993 #3] INFO -- : [9c80f545-4de0-4330-9771-b97f47c49d06] Completed 200 OK in 97ms (Views: 22.4ms | ActiveRecord: 50.1ms) 
2016-10-03T18:28:09.513597+00:00 app[web.1]: I, [2016-10-03T18:28:09.513474 #3] INFO -- : [85138d87-8923-459e-b5c4-0c8c8458fbf8] Started GET "/assets/application.scss" for 69.181.73.43 at 2016-10-03 18:28:09 +0000 
2016-10-03T18:28:09.539647+00:00 heroku[router]: at=info method=GET path="/assets/application.scss" host=seaboltapplication.herokuapp.com request_id=85138d87-8923-459e-b5c4-0c8c8458fbf8 fwd="69.181.73.43" dyno=web.1 connect=1ms service=9ms status=304 bytes=258 
2016-10-03T18:28:09.636363+00:00 heroku[router]: at=info method=GET path="/assets/application-8e81f96a4be820b7a898f27d43882ce589b4df24917971fd73bf945e1a4700db.js" host=seaboltapplication.herokuapp.com request_id=370fb212-f097-4afd-9651-0b5584845e17 fwd="69.181.73.43" dyno=web.1 connect=0ms service=1ms status=304 bytes=48 
2016-10-03T18:28:09.752359+00:00 heroku[router]: at=info method=GET path="/assets/mobile.scss" host=seaboltapplication.herokuapp.com request_id=69751179-3193-4419-8ba8-a21bea0e01ea fwd="69.181.73.43" dyno=web.1 connect=0ms service=9ms status=304 bytes=258 
2016-10-03T18:28:09.737788+00:00 app[web.1]: I, [2016-10-03T18:28:09.737698 #3] INFO -- : [69751179-3193-4419-8ba8-a21bea0e01ea] Started GET "/assets/mobile.scss" for 69.181.73.43 at 2016-10-03 18:28:09 +0000 

約翰 - 的MacBook-PRO:BookApp johnseabolt $

這裏是我的書控制器: 類BooksController中<的ApplicationController before_action:的authenticate_user!只: :new,:edit,:index,:show]

def index 
    @books = Book.all 
end 

def new 
    @book = current_user.books.new 
end 

def create 
    @book = current_user.books.build(book_params) 
    if @book.save 
     redirect_to books_path 
    else 
     render 'new' 
    end 
end 

def show 
    @book = Book.find(params[:id]) 
    if @book.reviews.blank? 
     @average_review = 0 
    else 
     @average_review = @book.reviews.average(:rating).round(2) 
    end 
end 

def edit 
    @book = Book.find(params[:id]) 
end 

def update 
@book = Book.find(params[:id]) 
if @book.update(book_params) 
    redirect_to book_path(@book) 
else 
    render "edit" 
end 
end 


def destroy 
    @book = Book.find(params[:id]) 
    @book.destroy 
    if @book.destroy 
     redirect_to books_path 
    else 
     render 'show_books_path' 
    end 
end 

private 
    def book_params 
     params.require(:book).permit(:description, :author, :category_id, :book_img, :title) 
    end 
end 

這裏是t的相關部分他預定的顯示頁面:

<div class="inner_left"> 
    <%= image_tag @book.book_img.url, :class => "book_show" %> 

     <div class="buttons"> 

      <% if user_signed_in? then %> 
       <% if current_user.id = @book.user_id %> 
        <%= link_to "Edit", edit_book_path, :class => "btn_left" %> 
        <%= link_to "Delete", book_path(@book), method: :delete, :class => "btn_right", data: {confirm: "Are you sure?"} %> 
       <% end %> 
      <% end %> 
      <br /> 
      <%= link_to "Back", root_path, :class => "btn" %> 
      <br /> 

      <%= link_to "Add Review", new_book_review_path(@book) , :class => "btn_submit" %> 
     </div> 
    </div> 

這裏是我的配置/ enivornments/prodection.rb文件的相關部分:

config.assets.compile = true 
+0

馬克以下爲正確的Dinesh更換

only: [:new, :edit, :index, :show] 

,您需要更改方法::在你的link_to中刪除視圖中的「刪除」。 – bkunzi01

+0

你的application.js怎麼樣?發佈你的application.js和application.html.erb – dnsh

+0

這個答案也可能適用於你:http://stackoverflow.com/a/21219515/2930161 – tkymtk

回答

0

變化method: :destroymethod: :delete

代替

<%= link_to "Delete", book_path(@book), method: :destroy, :class => "btn_right", data: {confirm: "Are you sure?"} %> 

使用

<%= link_to "Delete", book_path(@book), method: :delete, :class => "btn_right", data: {confirm: "Are you sure?"} %> 

如果仍然無法正常工作,這意味着你不加載JQuery-ujs。請確保你在application.js中

//= require jquery 
//= require jquery_ujs 
+0

道歉。我正在擺弄按鈕中的「刪除」與「摧毀」,看它是否有效。我實際上最初有「刪除」。它仍然不起作用。非常感謝您的快速響應!我編輯了我的原始文章,以更準確地反映我的代碼。你知道這可能是什麼嗎? –

0

在BooksController中類以下內容,嘗試用

only: [:new, :edit, :index, :show, :destroy]