2013-08-28 209 views
0

我是ROR的初學者,我正在關注本教程http://guides.rubyonrails.org/getting_started.htmlROR刪除操作不起作用

因此,根據本教程,我想刪除一篇文章。但它不工作就顯示這個錯誤The action 'destroy' could not be found for PostsController

我交控制器刪除方法看起來像

def destroy 
    @post = Post.find(params[:id]) 
    logger.debug "***********************: #{@post.id}" 
    @post.destroy 

    redirect_to posts_path 
end 

在路線我提到的資源resources :posts但它仍然是給錯誤破壞行動。難道我做錯了什麼。需要幫忙。

+1

你如何訪問您的破壞行動?一個相當常見的錯誤是將其作爲HTTP GET而不是HTTP DELETE。例如。 http://stackoverflow.com/questions/18467381/when-calling-destroy-record-is-not-being-removed-from-db/18467444#18467444 – Shadwell

+1

我看到'end'有一個不同的縮進:你確定它匹配'def'?或者它是關閉類定義? – Miotsu

+0

對不起這個可憐的問題。關於'end'。我現在做得很好,現在工作得很好。 – nilkash

回答

1

您是否在視圖中提及刪除方法?

如果您使用Rails 4,你應該做的:

<%=link_to 'Destroy', post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %> 

在Rails 3:

<%=link_to 'Destroy', post_path(post), method: :delete, confirm: 'Are you sure?' %>