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

时间:2015-01-21 15:05:11

标签: javascript ruby-on-rails ruby ruby-on-rails-4

我正在研究Michael Hartl的Ruby on Rails教程(我是一个菜鸟)。

在应用程序中我想删除微博,但每当我尝试在浏览器中执行它时,它会告诉我存在路由错误。 “没有路线匹配[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

非常感谢您提前回答。

1 个答案:

答案 0 :(得分:4)

确保你有

//= require jquery
//= require jquery_ujs

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

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

说明:RUBY 尝试是RESTful,因此它发送了edit动作的PATCH请求,删除destroy动作的请求。但大多数浏览器只能提交GET和POST表单。总是通过GET打开超链接(并且link_to生成<a href=...>标记)。因此rails会做一些hackery并“模拟”DELETE,PUT和PATCH请求。

form_tag helper创建了额外的隐藏输入:<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属性的链接的点击,并使用method="POST"创建隐藏的表单,是,使用name="_method" value="delete"隐藏输入,然后提交此表单。看看jquery_ujs source code,这很简单。

SO 如果您在点击method: :destroy链接后在服务器控制台中看到GET请求,则很可能是javascript存在问题。