Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 879
Luis Vivas Avatar asked Nov 21 '25 07:11

Luis Vivas


1 Answers

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).

In jQuery, you do something like:

$('#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. Ryan Bates' Railscasts series has several examples of using Javascript in RoR for dynamic content.

Also, NetTuts+ has this tutorial for using Unobtrusive JavaScript in Rails 3.

like image 117
Jason Lewis Avatar answered Nov 23 '25 20:11

Jason Lewis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!