在 Rails 4 应用程序中集成 Twitter Feed



我正在尝试将Twitter提要集成到我的rails four 4应用程序中,这是一个cms。我解决了推特宝石并成功安装了它。我在 application_controller.rb 中设置了以下内容。

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
require 'twitter'
  @client = Twitter::REST::Client.new do |config|
    config.consumer_key = 'my consumer key'
    config.consumer_secret = 'my consumer secret'
    config.access_token = 'access token'
    config.access_token_secret = 'access token secret'
  end
end

注意:我在此处省略了我的凭据,但在我的应用程序中正确使用了它们。然后我在我的视图中添加了以下内容,即应用程序.html.erb

<section id="tweets">
  <ul>
    <% @client.each do |tweet| %>
      <li><%=  tweet.text %></li>
    <% end %>  
  </ul>
</section>

我收到以下错误

undefined method `each' for nil:NilClass

我可能做错了什么?我已经通读了 Twitter gem 文档,从设置初始值设定项开始,意识到我在初始值设定项中的局部变量在我进入视图时失去了范围,所以我删除了它并在控制器中添加了初始化。请注意,这是@client 详细的答案将不胜感激。

所以我最终从我的推特帐户中使用了推特小部件。这是解决我问题的代码

<div class="span4">
  <a class="twitter-timeline" href="https://twitter.com/YourHandle" data-widget-id="MyTwitterId">Tweets by YourHandle</a>
  <script>
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
    if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js"
    ;fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
  </script>
</div>

注意:将你的手柄替换为你的推特句柄。我还省略了我的数据小部件 ID。要获得您的推特设置,请转到您的推特设置,然后创建推特小部件。将生成类似的代码,您可以将其嵌入到您的网站上。

最新更新