Luis Vivas
Luis Vivas

Reputation: 51

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.

Upvotes: 3

Views: 5978

Answers (1)

Jason Lewis
Jason Lewis

Reputation: 1314

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.

Upvotes: 4

Related Questions