2017-07-07 77 views
1

我按照此tutorial構建了一個簡單的博主。無法在Rails控制器中執行刪除操作

但是,我無法執行本教程所示的刪除功能。是

給出的步驟如下:

讓我們來定義我們ArticlesController的銷燬方法,因此:

  • 用途PARAMS [:ID]來查找數據庫中的文章

  • 致電該物件的破損

  • 重定向到藝術品icles索引頁

現在就自己做並測試它。

這是我的代碼:

class ArticlesController < ApplicationController 
    include ArticlesHelper 

    def index 
     @articles = Article.all 
    end 

    def show 
     @article = Article.find(params[:id]) 
    end 

    def new 
     @article = Article.new 
    end 

    def create 
     @article = Article.new(article_params) 
     @article.save 
     flash.notice = "Article '#{@article.title}' Created!" 
     redirect_to article_path(@article) 
    end 

    def destroy 
     @article = Article.find(params[:id]) 
     @article.destroy 
     redirect_to articles_path 
    end 
end 

show.html.erb

<h1><%= @article.title %></h1> 
<p><%= @article.body %></p> 
<%= link_to "<< Back to Articles List", articles_path %> 
<%= link_to "delete", article_path(@article), method: :delete %> 

article_helper.rb

module ArticlesHelper 

    def article_params 
    params.require(:article).permit(:title, :body) 
    end 

end 

我已經通過其他的解決方案瀏覽在線,但我不沒有看到我可能錯過的任何代碼。

就我而言,單擊刪除鏈接只會導致頁面刷新。

+2

爲什麼'私人'?向上移動 –

+0

謝謝,我正在關注github上的修改過的教程,我根據較新的教程更新了代碼,但它仍然無法正常工作。 – Carrein

+0

最新的錯誤? – puneet18

回答

1

我找到了問題:

安裝程序在Windows上運行。我之前申請了fix,因爲該頁面無法加載。

此修復程序導致這裏的錯誤:

的ActionController :: RoutingError(無路由匹配[GET] 「/javascripts/default.js」):

解決這個問題的辦法是撤消上面的更改並按照修復here

顯然,咖啡腳本源1.9及以上版本不適用於Windows。

+0

一個快速的建議,使用linux/ubuntu,當然MacOS也不錯.. :) –

2

private控制器中的方法無法在控制器外訪問,即使是路由。因此,您將無法在當前代碼中路由到destroyarticle_params。如果您將destroy方法移至private關鍵字以上,應該修正這些問題。

+0

謝謝,我正在關注github上的修改過的教程,我按照較新的教程更新了代碼,但它仍然無效。 – Carrein

相關問題