简体   繁体   中英

How to get javascript in html.erb file to render in rails 4 app?

I have a rails app that I am trying to add OpenTok to. OpenTok uses javascript, and when I go to my view party.html.erb nothing shows up besides the text in my beginning html tags.

<div id="invitation">Invite your friends! Share the url of this page   

localhost:3000/party/<%= @room.id %></div>
<div id="videobox">

</div>
<script src="//static.opentok.com/webrtc/v2.2/js/opentok.min.js" ></script>
<script type="text/javascript">
var apiKey = API_KEY;
var sessionId = <%= @room.sessionId %>;
var token = <%= @tok_token %>;



var session = OT.initSession(apiKey, sessionId);

  session.on("streamCreated", function(event) {
    session.subscribe(event.stream);
  });

  session.connect(token, function(error) {
    var publisher = OT.initPublisher();
    session.publish(publisher);
  });
</script>

</script>

Do I need a seperate javascript file? Thanks for your help.

if your OpenTok is related to some DOM elements, maybe you should wrap it to something like:

$(function(){
   ...
});

which will be triggered after loading full page.

Two ways you can include the java script

1) Download the java script file and put it in your app(app/assets/javascripts) and add this

<script type="text/javascript" src="/assets/opentok.min.js"></script>

your html.erb file

2)Get it from online

<script type="text/javascript" src="https://static.opentok.com/webrtc/v2.2/js/opentok.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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