Forum: Ruby on Rails Pass a ruby variable into js within Rails

B3249573a5345f4681c66a9430e8b3bc?d=identicon&s=25 Pierre-Andre M. (pierreandre_m)
on 2014-05-29 23:38
I have a variable created by some ruby in my controller that looks like
this:

@begpoint = row["begpoint"]


and I want to pass it into some js that is referenced from within my
view:

view:

  <% javascript_include_tag "play_time" %>



js (play_time):

I am trying to pass "<beg-point> into the js [obviously that <> is not
the right syntax just trying to denote where'd id like the variable to
end up]


var endtime = <beg-point>+10;
myPlayer= document.getElementById('example_video_1');
myPlayer.addEventListener('loadeddata', function(){
    myPlayer.currentTime = 10505.89;
    myPlayer.play();
    myPlayer.addEventListener('timeupdate', function(){
    if (myPlayer.currentTime >= endtime) {
        myPlayer.pause();
    }


Can anyone help?  Thanks

    }, false);

}, false);
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2014-05-30 01:44
Pierre-Andre M. wrote in post #1147586:
> I have a variable created by some ruby in my controller that looks like
> this:
>
> @begpoint = row["begpoint"]
>
>
> and I want to pass it into some js that is referenced from within my
> view:

There are several way of doing this, but the simplest is to use HTML5
data- attributes.

Example:

HTML:

<body data-begpoint="<%= @begpoint %>">
... page content
</body>

jQuery:

var begpoint;

$(funtion() {
  begpoint = $('body').data('begpoint');
});

So to think of is as "pass it to JavaScript" is sort of inside out. You
use JavaScript to "get" the value from the DOM. jQuery gives you nice
syntax for doing just that.

This is good for relatively small amount of data. If you need lots of
data in your JavaScript there are alternatives to this technique. A good
place to learn something about those techniques is something like
Backbone.js, Ember.js or Angular.

http://backbonejs.org
http://emberjs.com
https://angularjs.org
5756f9ea3fa0d1df985d7e2d4b83fa8a?d=identicon&s=25 Dave Sims (davesims)
on 2014-05-30 01:46
(Received via mailing list)
I've used the gon gem for this and liked it quite a bit.

https://github.com/gazay/gon
98156ca4d9d9229a2c967005966e8903?d=identicon&s=25 greatghoul (Guest)
on 2014-05-30 04:55
(Received via mailing list)
gon is great, and easy to use.

On Friday, May 30, 2014, Dave Sims <davsims@gmail.com> wrote:

> @begpoint = row["begpoint"]
>
>      myPlayer.currentTime = 10505.89;
>
> <javascript:_e(%7B%7D,'cvml','rubyonrails-talk@googlegroups.com');>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk...
>
<https://groups.google.com/d/msgid/rubyonrails-talk...
> .
> For more options, visit https://groups.google.com/d/optout.
>


--
 *greatghoul <http://www.g2w.me> - Ask and Learn!*
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.