【发布时间】:2015-07-24 20:50:26
【问题描述】:
我正在关注guides.rubyonrails.org 上的官方 Rails 指南,但遇到以下错误:
当我在浏览器地址栏中输入路径时,没有路由匹配 [GET] "/articles/8/destroy"。单击模板文件中的链接,不会删除带有传递 id 的文章,只是再次重定向到它(文章)。 Rails 版本为 4.2.1,数据库为 sqlite,工作环境为 Windows 7。
销毁方法如下:
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
Rake 路由输出为:
articles_path GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article_path GET /articles/new(.:format) articles#new
edit_article_path GET /articles/:id/edit(.:format) articles#edit
article_path GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root_path GET / welcome#index
我检查了拼写错误,甚至 c/p all source,结果都是一样的。和想法?
编辑:添加删除链接
删除链接如下:
<%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %>
views/layouts/中application.html.erb的内容如下:
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
javascript_include_tag 'default' 从 'application' 更改为 'defaul',否则我遇到了 coffeescript 错误。
【问题讨论】:
-
Rails 中的默认约定不允许您通过在地址栏中输入 URL 来删除记录。您可以发布包含“删除”链接的视图代码吗?
-
@jljohnstone,看编辑,你说得对,GET 不应该调用destroy 方法,忘了看路由。
-
当您单击
Delete链接时,您在rails 服务器输出中看到了什么?应该有某种错误可以帮助我们了解问题所在。 -
请发布您的
default.js或default.coffee文件。
标签: ruby-on-rails ruby