我是RoR的新手,但仍然没有足够的经验来解决我可能遇到的不同错误。在这种情况下,我正在设计一个博客,我可以发表文章。更具体地说,我的问题与删除这些文章有关。
据我所知,写作:
resources :articles
routes 文件中的是写作的替代方法:
get "/articles" #index
post "/articles" #create
delete "/articles/:id" #delete
get "/articles/:id" #show
get "/articles/new" #new
get "/articles/:id/edit" #edit
patch "/articles/:id" #update
put "/articles/:id" #update
当我尝试删除文章时,我收到以下错误:
我写的代码是:
查看
<% @articles.each do |art| %>
<%= art.title %>
<div>
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
</div>
<% end %>
控制器
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
我看不出我错过了什么。谢谢你提前。
答案 0 :(得分:1)
It sounds like you have this in your view:
<%= art.body %> - <%= link_to "Delete", art, method: :destroy %>
But you actually need:
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
I'd advise double-checking this in your app based on your reply to a comment from @GonzaloRobaina.