2017-04-02 34 views
0

我是rails on rails的初學者,當我嘗試刪除創建的對象時失敗。這裏是代碼。 controller無法在Ruby on rails上使用delete方法

class PuzzlesController < ApplicationController 
    def index 
    @puzzles = Puzzle.all 
    end 
    def show 
    @puzzle=Puzzle.find(params[:id]) 
    end 
    def new 
    @puzzle = Puzzle.new 

    end 
    def create 
     #render plain: params[:puzzle].inspect 
    @puzzle = Puzzle.new(puzzle_params) 
    if(@puzzle.save) 
     redirect_to @puzzle 
    else 
     render 'new' 
    end 
    end 
    def edit 
    @puzzle=Puzzle.find(params[:id]) 
    end 
    def update 
    @puzzle=Puzzle.find(params[:id]) 
    if(@puzzle.update(puzzle_params)) 
     redirect_to @puzzle 
    else 
     render 'edit' 
    end 
    end 
    def destroy 
    @puzzle = Puzzle.find(params[:id]) 
    @puzzle.destroy 

    redirect_to puzzle_path 
    end 
    private def puzzle_params 
    params.require(:puzzle).permit(:title,:body,:category,:difficulty) 
    end 
end 

show

<h2><%= @puzzle.title %></h2> 
    <p><%= @puzzle.body %></p> 
    <p><%= @puzzle.category %></p> 
    <p><%= @puzzle.difficulty %></p> 
<hr> 
<%= link_to "Edit", edit_puzzle_path(@puzzle), :class => 'btn btn-default'%> 
<%= link_to "Delete", puzzle_path(@puzzle), 
      method: :delete, 
      data: {confrim: 'are you sure? '}, 
      :class => 'btn btn-danger'%> 

當我刪除點擊,它像重新渲染頁面。我在互聯網上搜索了很多,無法找到解決方案。

這是rails server上的信息。

Started GET "/puzzles/1" for ::1 at 2017-04-02 18:51:18 +0930 
Processing by PuzzlesController#show as HTML 
    Parameters: {"id"=>"1"} 
    Puzzle Load (0.5ms) SELECT "puzzles".* FROM "puzzles" WHERE "puzzles"."id" = ? LIM    IT ? [["id", 1], ["LIMIT", 1]] 
    Rendering puzzles/show.html.erb within layouts/application 
    Rendered puzzles/show.html.erb within layouts/application (1.0ms) 
Completed 200 OK in 63ms (Views: 46.2ms | ActiveRecord: 0.5ms) 
+0

請將實際的代碼添加到問題主體。不要添加代碼的圖像。 –

+0

你可以在這裏添加你的代碼到你的問題嗎? – matov

+0

你使用什麼版本的Rails? – spickermann

回答

0

改變您的重定向路徑

redirect_to puzzles_path 
+0

改了,還是一樣的 – yue

0

有幾個問題在你的代碼:

1)是不是confrimlink_to參數列表(confirm一個錯字,):

<%= link_to 'Delete', puzzle_path(@puzzle), 
     method: :delete, 
     data: { confirm: 'are you sure? '}, 
     class: 'btn btn-danger' %> 

2)在您的結束時方法你不能重定向到一個奇異的謎題路徑,因爲你剛剛刪除了謎題。重定向到索引頁代替:

redirect_to puzzles_path 

3)但最重要的。鏈接method: 'delete'只適用於JavaScript和Rails 5.0取決於jQuery。因爲你寫的鏈接僅僅重定向到show頁面,我想你不已經包含了Rails的JavaScript文件到您的資產管道:

# Add this to the head of your `views/layout/application.rb`: 
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 

# Ensure that your `assets/javascripts/application.js` include the following lines: 
//= require jquery 
//= require jquery_ujs 

確保瀏覽器的檢測控制檯,該文件是沒有錯誤加載。

+0

謝謝你的幫助,我修正了1)和2)的錯誤。 3)rails javeScript已包含在application.js中// = require jquery // = require jquery_ujs // = require turbolinks // = require_tree。 在「views/layout/application.html.erb」的頭部,有一行<%= javascript_include_tag'default','data-turbolinks-track':'reload'%>。我是否需要將「默認」更改爲「應用程序」? – yue

+0

如果您的主JavaScript文件被命名爲'application'而不是'default',那麼您應該將'javascript_include_tag'改爲包含名爲'application'的文件。我很驚訝你在嘗試加載一個不存在的'default'文件時沒有找到'404(not found)'。 – spickermann

+0

該行出現錯誤。在Puzzles#show中執行ExecJS :: ProgramError。 TypeError:該對象不支持此屬性或方法。 – yue