Last active
August 29, 2015 14:20
-
-
Save nizaroni/a67a1b95c94d75bb8a29 to your computer and use it in GitHub Desktop.
Twitter in Rails: Showing posts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
# ... | |
# Add a get route for listing posts | |
get '/posts' => 'posts#index' | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
def index | |
# Make a new instance of the Twitter client | |
# (we will move this to it's own class later) | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV["twitter_consumer_key"] | |
config.consumer_secret = ENV["twitter_consumer_secret"] | |
# Use access_token info we already saved to the database | |
config.access_token = current_user.twitter_token | |
config.access_token_secret = current_user.twitter_token_secret | |
end | |
# Make a request to Twitter for the home_timeline | |
# Store that in an instance (@) variable for use in view/erb | |
@posts = client.home_timeline(count: 50) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Your posts</h1> | |
<%# Have an ordered list wrapper for the list of posts %> | |
<ol> | |
<%# Loop through the @posts we got from Twitter %> | |
<% @posts.each do |post| %> | |
<%# Each post has a list item wrapper %> | |
<li> | |
<%# Print out the post's user's name %> | |
<strong>@<%= post.user.name %>:</strong> | |
<%# Print out the post's text %> | |
<p><%= post.text %></p> | |
</li> | |
<% end %> | |
</ol> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment