如何使用twitter gem在rails中显示twitter用户feed

时间:2014-05-16 18:01:58

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

我使用twitter gem for rails尝试显示来自我的Twitter Feed的3个状态。我按照文档做了所有事情,但在我的视图中没有显示任何内容。

在我的应用程序控制器中,我已经解决了这个问题:

client = Twitter::REST::Client.new do |config|
 config.consumer_key        = "***********"
  config.consumer_secret     = "***********"
  config.access_token        = "***********"
  config.access_token_secret = "***********" 

 end



def client.get_all_tweets(user)
    options = {:count => 3, :include_rts => true}
      user_timeline(user, options)
  end


  @tweet_news = client.get_all_tweets("tezzataz")

然后在我看来,我简单地说:

<% @tweet_news %>

我没有收到任何错误,但我的观点中没有显示任何内容。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

用于此目的。

<ul>
  <% @tweet_news.each do |f| %>
    <li>
      <%= f.text%>
      <span><%= time_ago_in_words(f.created_at) %> ago</span>
    </li>
  <% end %>
</ul>

答案 1 :(得分:0)

Controller

require 'twitter'

class TweetController < ApplicationController
  def index
    client = TweetController.create_client
    begin
      @tweets = get_tweets(client)
    rescue => e
      #TODO: render 404 with the error
       puts "Error : #{e.to_s}"
    end
  end

  private
  def get_tweets(client, userName)
    client.user_timeline(userName, :count => 200)
  end
end

View

<div class="row">
    <div class="col-lg-12">
        <ul class="timeline">
            <% @tweets.each do |tweet| %>
                    <div class="timeline-image">
                        <!-- Show user avatar (profile pic)-->
                        <img class="img-circle" src= '<%= image_path(tweet.user.profile_image_url_https.to_s.gsub('_normal','')) %>' alt="" style="width: 100%;height: 100%;">
                    </div>
                    <div class="timeline-panel">
                        <div class="timeline-heading">
                            <!-- Show user name -->
                            <h4> <%= tweet.user.name %></h4>
                            <h4 class="subheading"> <%= "@#{tweet.user.screen_name}"  %> </h4>
                        </div>
                        <div class="timeline-body">
                            <p class="text-muted"> <%= tweet.text%> </p>
                            <i class="fa fa-retweet"> <%= tweet.retweet_count %> </i>
                            <i class="fa fa-heart"> <%= tweet.favorite_count %> </i>                                        
                        </div>
                    </div>
                </li>
            <% end %>
        </ul>
    </div>
</div>