【问题标题】:RoR routing error - No route matches [GET] "/articles/8/destroy"RoR 路由错误 - 没有路由匹配 [GET] "/articles/8/destroy"
【发布时间】: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.jsdefault.coffee 文件。

标签: ruby-on-rails ruby


【解决方案1】:

找不到路线说明

您的路线显示DELETE /articles/:id(.:format) articles#destroy。这意味着它期待 HTTP DELETE 方法。

为了让/articles/8/destroy 工作,你的路由应该这样定义:

get '/articles/:id/destroy' => 'articles#destroy'

但是,这不是推荐的方法,而是确保您在your delete link 中设置删除方法。

手动测试

如果您想手动测试,可以尝试curl 或 chrome 插件postman 之类的工具。

这两种工具都可以让您指定要使用的 HTTP 方法。如果您通过浏览器发出直接请求,您所做的就是发送GET 请求。

【讨论】:

    【解决方案2】:

    确保在您的应用程序中包含用于 jQuery 的 Rails 非侵入式脚本适配器。否则,使用method: :delete 创建的链接将无法正确处理。

    这是通过 JavaScript 文件 jquery_ujs 完成的,当您生成应用程序时,该文件会自动包含在应用程序的布局 (app/views/layouts/application.html.erb) 中。

    【讨论】:

      【解决方案3】:

      当你在你的 url 中点击这个 /articles/8/destroy 时,它被认为是 get 请求,因为你可以看到错误。 没有路线匹配 [GET] "/articles/8/destroy"

      但 DESTROY 操作在 rails 中接受 delete 请求,您也可以在路线中看到。现在我不确定你是如何从模板触发的,但考虑到你的文章在对象 article

      中,它应该是这样的
        <%= link_to "Delete", article, method: :delete, data: { confirm: 'Are you sure?' } %>  
      

      【讨论】:

        【解决方案4】:

        正如这一行所说:

        DELETE  /articles/:id(.:format)         articles#destroy
        

        服务器期望DELETE 请求触发控制器中的destroy 方法。像这样的事情,从每个导轨上的指南都应该做:

        <%= link_to 'Destroy',  @article,  method: :delete, 
          data: { confirm: 'Are you sure?' } %>
        

        link_to 助手和 delete 方法创建一个隐藏表单并在点击时提交。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-14
          • 2014-03-26
          • 2016-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-24
          相关资源
          最近更新 更多