David Ryder
David Ryder

Reputation: 1311

RoR - uninitialized constant Twitter::Client

twitter_helper.rb

module TwitterHelper

  require 'rubygems'
  require 'twitter'
  require 'net/http'
  require 'uri'


  def get_tweets (twitter_user)
    begin
        @tweets = Array.new
        @twitter = Twitter::Client.new

        Twitter.user_timeline(twitter_user) do |tweet|
            @tweets.push(tweet)
        end
        @tweets
    rescue Exception => e
      puts e
      _("Errors: #{e.to_s}
        ")
    end
  end

end

My gemfile includes

gem 'twitter'

And I've done

bundle install

But every time I call

tweets = get_tweets

From my view I get the exception (uninitialized constant Twitter::Client). What am I missing?

Upvotes: 3

Views: 1575

Answers (2)

sameera207
sameera207

Reputation: 16629

It's quite unusual to have the imports inside the module, have them outside and see

require 'rubygems'
require 'twitter'
require 'net/http'
require 'uri'

module TwitterHelper
  def get_tweets (twitter_user)
    begin
        @tweets = Array.new
        @twitter = Twitter::Client.new

        Twitter.user_timeline(twitter_user) do |tweet|
            @tweets.push(tweet)
        end
        @tweets
    rescue Exception => e
      puts e
      _("Errors: #{e.to_s}
        ")
    end
  end

end

Upvotes: 1

David Ryder
David Ryder

Reputation: 1311

I hate answering my own questions, but here goes.

Check the version number. Significant changes were made between the twitter gem <1.0 and >=1.0. You should specify

gem 'twitter', '>= 1.0'

And then

bundle install

And resolve any dependency conflicts (if you get any).

Upvotes: 1

Related Questions