【问题标题】:Ruby DELETE method is recognised as GETRuby DELETE 方法被识别为 GET
【发布时间】:2020-11-28 09:15:30
【问题描述】:

我开始使用 Ruby 并从他们的官方资料中实现简单的博客。

我完成了应用程序,但在破坏操作时遇到了问题。这是表格

<h1>Listing Articles</h1>
 
<%= link_to 'New article', new_article_path %>

<% if !@articles.nil? %>
  
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</table>

<% else %>
  
  <p> no avaiable articles </p>

<% end %>

那是可用的路线

          Prefix Verb   URI Pattern                                  Controller#Action
   welcome_index GET    /welcome/index(.:format)                     welcome#index
            root GET    /                                            welcome#index
article_comments GET    /articles/:article_id/comments(.:format)     comments#index
                 POST   /articles/:article_id/comments(.:format)     comments#create
 article_comment GET    /articles/:article_id/comments/:id(.:format) comments#show
                 PATCH  /articles/:article_id/comments/:id(.:format) comments#update
                 PUT    /articles/:article_id/comments/:id(.:format) comments#update
                 DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
        articles GET    /articles(.:format)                          articles#index
                 POST   /articles(.:format)                          articles#create
     new_article GET    /articles/new(.:format)                      articles#new
    edit_article GET    /articles/:id/edit(.:format)                 articles#edit
         article GET    /articles/:id(.:format)                      articles#show
                 PATCH  /articles/:id(.:format)                      articles#update
                 PUT    /articles/:id(.:format)                      articles#update
                 DELETE /articles/:id(.:format)                      articles#destroy

您可能会注意到表单链接上的delete 方法和DELETE 请求映射到articles#destroy 操作。但是,单击此链接时会调用 GET 而不是 DELETE(相同的 URI 模式),但我还没有找到根本原因。

路由本身在下面

Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  
  get 'welcome/index'
  
  root 'welcome#index'

  post '/articles/:id', to: 'articles#update'

  resources :articles, only: [:new, :edit, :index, :create, :show, :update, :destroy] do 
    resources :comments
  end

end

我成功地为显式暴露的路由定义别名,但除了引入新问题之外,它没有使用平台原生方式resources :articles

您能否帮助了解\找出此问题的原因?

【问题讨论】:

  • 我猜&lt;td&gt;&lt;%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt; 工作?

标签: ruby


【解决方案1】:

问题是link_to辅助标签需要UJS来处理删除,请确保将其添加到application.js文件中

//= require jquery
//= require jquery_ujs

如果你需要在 rails 6 中

require("@rails/ujs").start()

【讨论】:

  • 感谢您的意见。它没有帮助。如果您想更深入地研究这个问题,我将我的代码放入 repo github.com/Gelassen/ruby-samples-and-interviews。 (附言我的应用程序没有资产/javascripts/application.js,它根本没有资产)
  • 是的,我明白了,你已经用 API 模式初始化了应用程序,所以它没有资产目录,这就是为什么你的 link_to 帮助器的删除方法不能正确工作
  • 现阶段是否可以在 Ruby 中将项目转换为其他类型?
  • 是的,你只需要运行rails new ruby-samples-and-interviews ,你可以阅读详细信息here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2022-11-13
  • 2021-05-27
  • 2015-09-10
  • 2013-08-26
相关资源
最近更新 更多