I am following along with the documentation for the Twitter gem and trying to get some results, but I am getting the following error:
undefined method `sample' for nil:NilClass
I've put this in my config/initializers/twitter_credentials.rb (my keys and tokens are filled in my app)
@client = Twitter::Streaming::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
Here are bits from my controller:
require 'twitter'
class StaticPagesController < ApplicationController
def home
@tweets = Array.new
@client.sample do |object|
@tweets << object.text if object.is_a?(Twitter::Tweet)
end
end
end
This is in my view:
<% @tweets do |tweet| %>
<%= tweet.text %>
<% end %>
And of course, this is in my gemfile:
gem 'twitter'
Why am I getting that error?
The problem was that I was referencing it using an instance variable (e.g. @client =
), when I needed to make it a constant or a global.
Answer CLIENT =
The other option was to put it instantiate the client within the controller.