【问题标题】:Integrating Twitter Feed in Rails 4 App在 Rails 4 应用程序中集成 Twitter 订阅源
【发布时间】:2014-01-16 17:01:16
【问题描述】:

我正在尝试将 twitter 提要集成到我的 rails 4 4 应用程序中,这就是 cms。我解决了 twitter gem 并成功安装了它。 我在 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

注意:我在这里省略了我的凭据,但在我的应用程序中正确保存了它们。 然后我在我的视图中添加了以下内容,即 application.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 详细的答案将不胜感激。

【问题讨论】:

  • 我的回答有帮助吗?
  • @majioa 不是真的。实际上,我决定使用我的 Twitter 帐户中的 twitter 小部件。它为我解决了这个问题。
  • 是谁?如果你自己得到答案,写在下面%)

标签: ruby-on-rails twitter twitter-gem


【解决方案1】:

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

<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>

注意:将 YourHandle 替换为您的 twitter 句柄。我还省略了我的数据小部件 ID。让你去你的推特设置然后创建推特小部件。将生成一个类似的代码,您可以将其嵌入到您的站点中。

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多