嗨
我刚刚看到一篇关于如何将 Rails 与 jQuery 结合起来的文章。
http://blog.bernatfarrero.com/category/ror/
我按照说明进行操作,但无法使其工作(而不是使用 ajax Rails 尝试使用非 javascript 方式来处理请求)。下载源代码后我发现了问题所在。在 public/javascripts 的源代码中有一个文件 jquery-rails.js 与行连接
javascript_include_tag "jquery-rails.js"
在application.html.erb中。如果我在上面的行中更改该文件的名称,它将不再起作用。所以我尝试更改从此处下载的驱动程序文件的原始名称:http://docs.jquery.com/Downloading_jQuery#Download_jQuery (许多不同的版本)到我在源中找到的版本。当然我也把它放在同一个目录中。它再次失败......所以最后我比较了文件。我的意思是源文件要短得多。嗯,我是 JS 的真正初学者,所以我必须在这里向你寻求帮助。为什么不起作用?如何使 Rails 与原始 jQuery 驱动程序一起工作?
再见
下面您可以看到源代码中的“jquery-rails.js”
jQuery(function ($) {
var csrf_token = $('meta[name=csrf-token]').attr('content'),
csrf_param = $('meta[name=csrf-param]').attr('content');
$.fn.extend({
/**
* Triggers a custom event on an element and returns the event result
* this is used to get around not being able to ensure callbacks are placed
* at the end of the chain.
*
* TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
* own events and placing ourselves at the end of the chain.
*/
triggerAndReturn: function (name, data) {
var event = new $.Event(name);
this.trigger(event, data);
return event.result !== false;
},
/**
* Handles execution of remote calls firing overridable events along the way
*/
callRemote: function () {
var el = this,
data = el.is('form') ? el.serializeArray() : [],
method = el.attr('method') || el.attr('data-method') || 'GET',
url = el.attr('action') || el.attr('href');
if (url === undefined) {
throw "No URL specified for remote call (action or href must be present).";
} else {
if (el.triggerAndReturn('ajax:before')) {
$.ajax({
url: url,
data: data,
dataType: 'script',
type: method.toUpperCase(),
beforeSend: function (xhr) {
el.trigger('ajax:loading', xhr);
},
success: function (data, status, xhr) {
el.trigger('ajax:success', [data, status, xhr]);
},
complete: function (xhr) {
el.trigger('ajax:complete', xhr);
},
error: function (xhr, status, error) {
el.trigger('ajax:failure', [xhr, status, error]);
}
});
}
el.trigger('ajax:after');
}
}
});
/**
* confirmation handler
*/
$('a[data-confirm],input[data-confirm]').live('click', function () {
var el = $(this);
if (el.triggerAndReturn('confirm')) {
if (!confirm(el.attr('data-confirm'))) {
return false;
}
}
});
/**
* remote handlers
*/
$('form[data-remote]').live('submit', function (e) {
$(this).callRemote();
e.preventDefault();
});
$('a[data-remote],input[data-remote]').live('click', function (e) {
$(this).callRemote();
e.preventDefault();
});
$('a[data-method]:not([data-remote])').live('click', function (e){
var link = $(this),
href = link.attr('href'),
method = link.attr('data-method'),
form = $('<form method="post" action="'+href+'"></form>'),
metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
if (csrf_param != null && csrf_token != null) {
metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
}
form.hide()
.append(metadata_input)
.appendTo('body');
e.preventDefault();
form.submit();
});
/**
* disable-with handlers
*/
var disable_with_input_selector = 'input[data-disable-with]';
var disable_with_form_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')';
$(disable_with_form_selector).live('ajax:before', function () {
$(this).find(disable_with_input_selector).each(function () {
var input = $(this);
input.data('enable-with', input.val())
.attr('value', input.attr('data-disable-with'))
.attr('disabled', 'disabled');
});
});
$(disable_with_form_selector).live('ajax:after', function () {
$(this).find(disable_with_input_selector).each(function () {
var input = $(this);
input.removeAttr('disabled')
.val(input.data('enable-with'));
});
});
});
最佳答案
我假设您使用的是rails 3,如果是这样,使用jquery的最简单方法是使用jquery-rails gem 只需将其安装(添加 gem 'jquery-rails'
)到您的 Gemfile,运行 bundle
然后运行 railsgenerate jquery:install
并且 jquery 可以被使用!
关于jquery - Ruby on Rails 与 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5293495/