繁体   English   中英

Ruby DELETE方法被识别为GET

[英]Ruby DELETE method is recognised as GET

我开始使用 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

你能帮助理解\找到这个问题的原因吗?

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

//= require jquery
//= require jquery_ujs

如果在rails 6中你需要

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM