安装JQuery时,为什么要“删除"?链接中断? [英] When installing JQuery, why does my "delete" link break?

查看:52
本文介绍了安装JQuery时,为什么要“删除"?链接中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以创建和删除产品的Rails 3应用程序.索引页列出了所有产品.列表中每个产品的旁边都有一个删除链接:

I have a Rails 3 application that can create and delete products. An index page lists all the products. Next to each product in the list is a delete link:

<%= link_to "Delete", product, :method => :delete, :title => "Delete" %>

在HTML中如下所示:

This looks like this in the HTML:

<a href="/products/104" data-method="delete" rel="nofollow" title="Delete">Delete</a>

一切正常.

我已经到了要在应用程序中安装JQuery的地步.我遵循了几个网页上的可用步骤(例如 this )

I've reached the point where I want to install JQuery in my application. I followed the steps available on several web pages (like this or this, for example).

安装JQuery后,上面的删除"链接断开. Rails不会处理ProductsController中的destroy动作,而是尝试处理show动作!

After installing JQuery, the above "delete" link breaks. Instead of processing the destroy action in ProductsController, Rails attempts to process the show action!

我的猜测是,这与以下事实有关:Web浏览器无法本地发送DELETE请求,因此Rails使用JavaScript伪造"了此类请求,这就是为什么我在以下行之前出现了 尝试安装JQuery:

My guess is that it has something to do with the fact that web browsers can’t send DELETE requests natively, so Rails "fakes" such requests with JavaScript, which is why I had the following line before attempting the JQuery install:

<%= javascript_include_tag :defaults %>

必须在安装JQuery期间进行某些操作,以使用于伪造" DELETE请求的Javascript不再起作用,从而导致调用show动作.但是我不知道到底发生了什么...

Something during the JQuery install must have made it such that the Javascript used to "fake" the DELETE request no longer works, resulting in the show action being called. But what exactly is happening I don't know...

有人有什么想法吗?谢谢.

Anyone have any ideas? Thank you.

推荐答案

仔细检查包含JavaScript文件的顺序.具体而言,必须在jQuery Rails驱动程序之前加载jQuery(请参见

Double-check the order that your JavaScript files are being included. Specifically, jQuery must be loaded before the jQuery Rails driver (see the following lines at http://github.com/BinaryMuse/rails-book/blob/master/app/views/layouts/application.html.haml#L6-L7):

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
<%= javascript_include_tag "rails" %>

(这假定您用 http://github.com/rails/jquery-ujs/blob/master/src/rails.js ).

这篇关于安装JQuery时,为什么要“删除"?链接中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆