2016-10-03 82 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]