I'm trying to integrate twitter feed into my rails four 4 app, a cms for that matter. I settled of twitter gem and had it successfully installed. I set up the following in 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
Note: I've omitted my credentials here but have them correctly in my app. Then I added the following in my views i.e application.html.erb
<section id="tweets">
<ul>
<% @client.each do |tweet| %>
<li><%= tweet.text %></li>
<% end %>
</ul>
</section>
I get the following error
undefined method `each' for nil:NilClass
What could I be doing wrong? I've read through the twitter gem documentation, started off by setting up an initializer, realized my local variable in the initializer lost scope by the time I was in the views so I deleted it and added the initialization in the controller. Note it's @client
A detailed answer would be appreciated.
So I ended up using twitter widget form my twitter account. Here's the code that solved my problem
<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>
NOTE: Replace YourHandle with your twitter handle. I've also omitted my data widget id. To get yours go to your twitter settings then create twitter widget. A similar code will be generated which you can embed on your site.