ruby on rails - Trouble Getting Twitter Gem To Work - Giving Undefined Method Errors -
i following along documentation twitter gem , trying results, getting following error:
undefined method `sample' nil:nilclass
i've put in config/initializers/twitter_credentials.rb (my keys , tokens filled in app)
@client = twitter::streaming::client.new |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 bits controller:
require 'twitter' class staticpagescontroller < applicationcontroller def home @tweets = array.new @client.sample |object| @tweets << object.text if object.is_a?(twitter::tweet) end end end
this in view:
<% @tweets |tweet| %> <%= tweet.text %> <% end %>
and of course, in gemfile:
gem 'twitter'
why getting error?
the problem was referencing using instance variable (e.g. @client =
), when needed make constant or global.
answer client =
the other option put instantiate client within controller.
Comments
Post a Comment