简体   繁体   English

在rails上使用Javascript在Ruby上

[英]Using Javascript on Ruby on rails

I am trying to use update some calculations everytime a person modifies a text field. 每次修改文本字段时,我都会尝试使用更新一些计算。 I have many question about how to know the id text field, how to store it in a variable, etc. I am a newbie with ruby and javascript and there are many questions. 我有很多关于如何知道id文本字段,如何将它存储在变量中的问题等等。我是ruby和javascript的新手,有很多问题。 Could you please tell me where I can find more information that explains step by step how to proceed with this? 您能告诉我在哪里可以找到更多信息,逐步解释如何继续这样做?

Thank you very much. 非常感谢你。

I highly recommend using jQuery with Ruby on Rails... it makes this sort of thing much easier, and is going to be the standard as of the next version (3.1). 我强烈建议在Ruby on Rails中使用jQuery ...它使这种事情变得更容易,并且将成为下一版本(3.1)的标准。

In jQuery, you do something like: 在jQuery中,你做了类似的事情:

$('#my_field').change(function() {
  $('#result').val(function() {
    do something...
  });
});

And you can use a js.erb template to embed Ruby in the JS to get data from the controller. 您可以使用js.erb模板在JS中嵌入Ruby以从控制器获取数据。 Ryan Bates' Railscasts series has several examples of using Javascript in RoR for dynamic content. Ryan Bates的Railscasts系列有几个在RoR中使用Javascript来动态内容的例子。

Also, NetTuts+ has this tutorial for using Unobtrusive JavaScript in Rails 3. 此外,NetTuts +有本教程在Rails 3中使用Unobtrusive JavaScript。

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

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