如何在 RoR 4 中向浏览器发送 DELETE 请求

How to send a DELETE Request to the browser in RoR 4

我正在编写 Michael Hartl 的 Ruby 教程(我是菜鸟)。

在应用程序中我想删除一条微博,但是当我尝试在浏览器中执行它时,它告诉我存在路由错误。 "No route matches [GET] "/microposts/3

这是我在 _micropost.html.erb

中的代码
<tr>
    <td class="micropost">
        <span class="content"><%= micropost.content %></span>
        <span class="timestamp">
            Posted <%= time_ago_in_words(micropost.created_at) %> ago
        </span>
    </td>
    <% if current_user?(micropost.user) %>
    <td>
        <%= link_to "delete", micropost, :method => :delete,
                                         :confirm => "You sure?",
                                         :title => micropost.content %>
    </td>
    <% end %>
</tr>
</tr>

我已经有了Javascript根据书本伪造请求的代码

<head>
  <title><%= title %></title>
  <%= csrf_meta_tag %>
  <%= render 'layouts/stylesheets' %>
  <%= javascript_include_tag :all %>
</head>

这是我的一部分 routes.rb

Rails.application.routes.draw do

  get 'sessions/new'

  resources :users
  resources :sessions, :only => [:new, :create, :destroy]
  resources :microposts, :only => [:create, :destroy]

  match '/signup', :to => 'users#new', via: [:get, :post]
  match '/signin', :to => 'sessions#new', via: [:get, :post]
  match '/signout', :to => 'sessions#destroy', via: [:get, :post]
  match '/contact', :to => 'pages#contact', via: [:get, :post]
  match '/about', :to => 'pages#about', via: [:get, :post]
  match '/help', :to => 'pages#help', via: [:get, :post]
  match '/:id', :to => 'users#show', via: [:get, :post]

  root :to => 'pages#home'

这是application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

还有 micropost_controller.rb

class MicropostsController < ApplicationController

    before_filter :authenticate, :only => [:create, :destroy]
    before_filter :authorized_user, :only => :destroy

    def create
        @micropost = current_user.microposts.build(micropost_params)
        if @micropost.save
            flash[:success] = "Micropost created!"
            redirect_to root_path
        else
            @feed_items = []
            render 'pages/home'
        end
    end

    def destroy
        @micropost.destroy
        redirect_back_or root_path
    end

    private
        def micropost_params
            params.require(:micropost).permit(:content)
        end

        def authorized_user
            @micropost = Micropost.find(params[:id])
            redirect_to root_path unless current_user?(@micropost.user)
        end
end

我找到了这个答案,但是 "button_to" 方法似乎没有解决我的问题:Delete link sends "Get" instead of "Delete" in Rails 3 view

非常感谢您的回答。

确保你有

//= require jquery
//= require jquery_ujs

在你的 application.js 中。还要在页面加载后查看浏览器的 javascript 控制台。也许有一些 javascript 错误,它们会阻止 jquery_ujs 工作。

此外,请注意,您需要将 :confirm => "You sure?" 更改为 :data=> {:confirm => "You sure?"}

解释: RUBY 正在尝试 成为 RESTful,因此它正在发送 [=] 的 PATCH 请求14=] 操作,删除 destroy 操作的请求。但大多数浏览器只能提交 GET 和 POST 表单。 Hyper-links 总是通过 GET 打开(并且 link_to 生成 <a href=...> 标签)。所以 rails 做一些 hackery 和 "emulate" DELETE、PUT 和 PATCH 请求。

form_tag 助手创建额外的隐藏输入:<input name="_method" type="hidden" value="delete" />,然后 Rails 解析请求参数并假定它是 DELETE 请求。你可以阅读它 in documentation.

link_to 'delete', '/some/url', :method => :delete 将依次生成以下 html:<a href="/some/url/" data-method="delete">delete</a>。然后 jquery_ujs javascript 使用 data-method 属性拦截所有对 link 的点击,并使用 method="POST" 创建隐藏表单,是的,使用 name="_method" value="delete" 隐藏输入,然后提交此表单。看一下jquery_ujssource code,比较简单

SO 如果您在使用 method: :destroy 单击 link 后在服务器控制台中看到 GET 请求,很可能是 [=53= 存在一些问题].