2011-04-11 34 views
0

回到Rails 2,link_to_remote中有一个:condition选项,它允许你指定一些必须返回true的javascript或远程请求不会被发送。但是,在rails 3中,这个选项似乎已被删除。Rails 3远程链接:condition和mootools

我想要做的是有一个ajax表单,但它需要在浏览器上提交之前进行验证(这是非常非标准的验证)。基本上我需要能够设置一些有条件的JavaScript表达式,以防止在验证失败时提交表单 - 与rails 2中的:condition选项相同。有没有什么方法可以在rails 3中再次实现相同的功能?

我正在使用mootools与rails.js的mootools版本。我查看了mootools的rails.js文件,并没有看到任何看起来像它会让我指定一些有条件的JavaScript的东西。我错过了什么吗?

*注:我只是通过源代码来查看原型中编写的默认rails.js文件。在这个文件中有有下面几行:

var event = element.fire("ajax:before"); 
if (event.stopped) return false; 

这看起来像在原型版本,你可以阻止通过听取被发送的请求“AJAX:之前”事件,并在监听停止事件。然而,我在mootools rails.js代码中找不到这样的东西,所以也许这是mootools rails.js文件中的一个错误?

回答

0

link_to_remote已从Rails 3中删除。现在是link_to :remote => true。如果你想要做一些JS验证,你可以使用这样的事情:

<%= link_to "test", '/test', :id => "test-link", :remote => true %> 

,并在application.js中添加验证(在我的例子是原型它不是通过MooTools做的一个问题。我猜的):

document.observe("dom:loaded", function() { 
    $('test-link').observe('click', function(event){ 
    if your_validation_magic 
     event.stop(); 
    }); 
}); 

更新: MooTools有这个活动太: https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js#L131