简体   繁体   English

了解jQuery Rails Gem和rails.js

[英]Understanding jQuery Rails Gem and rails.js

I'm trying to solve the book "Rails 3 in Action" where the authors have used the following code to add a link that uses ajax to generate a new file field. 我正在尝试解决“ Rails 3 in Action”这本书,作者使用下面的代码添加了一个链接,该链接使用ajax生成新的文件字段。 I'm having difficulty in understanding in how this works and what is the equivalent javascript(or jQuery) which can be functionally equivalent to understand it better: 我很难理解它是如何工作的,什么是等效的javascript(或jQuery),可以在功能上等效以更好地理解它:

 <%= link_to "Add another file", new_file_path,
     :remote => true,
     :update => "files",
     :position => "after"
 %>

Thanks a lot 非常感谢

Well I'm not sure if we can explain it better than the book. 好吧,我不确定我们能否比这本书更好地解释它。 Which part are you having trouble with? 您在哪部分遇到问题?

Your code will be turned into an 'a' link, the href for which will be the url for the new_file_path (probably the new method in the file controller). 您的代码将变成一个'a'链接,其href将是new_file_path的URL(可能是文件控制器中的new方法)。 You can examine this path by calling this method in the the rails console by typing app.new_file_path 您可以通过在rails控制台中键入app.new_file_path来调用此方法来检查此路径。

The 'remote' parameter will cause aa data-remote parameter to the 'a' element with value true, meaning it will be an ajax call. 'remote'参数将导致'a'元素的data-remote参数值为true,这意味着它将是ajax调用。 When the page loads rails.js will find the remote parameter and, as a result, bind a click event to an ajax call. 页面加载时,rails.js将找到远程参数,并因此将click事件绑定到ajax调用。

The location to update when the ajax call returns will be the 'files' element on the page. 当ajax调用返回时更新的位置将是页面上的'files'元素。

The position parameter indicates--don't update the files element directly--but put the response in the DOM right after that element. position参数表示-不要直接更新files元素-而是将响应放在该元素之后的DOM中。

If that doesn't help try these resources: 如果这样做没有帮助,请尝试以下资源:

http://guides.rubyonrails.org/ajax_on_rails.html http://guides.rubyonrails.org/ajax_on_rails.html

http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/

I'm very confused with that example, because it is incorrect and has nothing to do with Rails 3. Instead authors use somewhat odd syntax from link_to_remote , which is no longer supported in Rails 3. 我对该示例感到非常困惑,因为它是不正确的,并且与Rails 3无关。相反,作者使用link_to_remote的某种奇怪语法,在Rails 3中不再支持该语法。

This helper will generate the link below: 该帮助器将生成以下链接:

<a update="files" position="after" data-remote="true" href="/files/new">Add another file</a>

which simply fires an AJAX GET request to the /files/new url (because jquery-ujs observes clicks on links with data-remote="true" and produces such requests). 只是向/files/new url触发AJAX GET请求(因为jquery-ujs观察到对带有data-remote="true"链接的点击并产生了此类请求)。 It doesn't send any additional params (neither update , nor position ) to a server. 它不会向服务器发送任何其他参数( updateposition )。 The subsequent DOM manipulation is completely up to the response you generate on a server. 后续的DOM操作完全取决于您在服务器上生成的响应。

Therefore in Rails 3 update and position options is completely useless, if you want to communicate with a server (those may be used in a client side event handler, but it's not that book said about, as I understood). 因此,在Rails 3中,如果您想与服务器进行通信,则updateposition选项完全没有用(可以在客户端事件处理程序中使用,但据我所知,这不是那本书)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM