将Facebook Javascript SDK添加到我的Rails应用程序中

时间:2013-04-22 11:16:02

标签: ruby-on-rails facebook-javascript-sdk

我正在尝试使用Facebook Javascript SDK,现在我只是将此代码粘贴到具有类似按钮的erb模板中:

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/es_LA/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

然而,在Facebook开发人员文档中,据说这样的东西在正文开放标记之后才被直接推荐:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : 'YOUR_APP_ID',                        // App ID from the app dashboard
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
  };

  // Load the SDK asynchronously
  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

所以,我的问题是:

- &GT;我应该在哪里放置此代码?在我的布局或Javascript文件中,使用jQuery完成所有这些逻辑?

1 个答案:

答案 0 :(得分:0)

将代码放入主模板的底部,例如application.html.erb或base.html.erb [如果你有从应用程序控制器继承的基本控制器]