Reputation: 267
I have a Ruby on Rails application (Ruby-1.9, Rails-3.2)which integrates with twitter to display the latest tweets containing a particular "keyword" dynamically. But it throws an error as NameError in TweetsController#create uninitialized constant Twitter::Search on the browser . I have run the db migrations, restarted the server and tried for various options available on the net. But nothing seems to work. Can anyone help to resolve this error ?
The model and controller files are below
Tweet.rb (model file)
class Tweet < ActiveRecord::Base
def self.get_latest_new_year_resolution_tweets(keyword)
search = Twitter::Search.new
search.containing(keyword).result_type("recent").per_page(100).fetch.each do |tweet_results|
twitter_created_at = DateTime.parse(tweet_results.created_at)
unless Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', DateTime.parse(tweet_results.created_at), tweet_results.from_user_id_str])
Tweet.create!({
:from_user => tweet_results.from_user,
:from_user_id_str => tweet_results.from_user_id_str,
:profile_image_url => tweet_results.profile_image_url,
:text => tweet_results.text,
:twitter_created_at => twitter_created_at
})
end
end
end
end
TweetsController
class TweetsController < ApplicationController
def index
end
def create
String strText = params[:tweet][:search].to_s
Tweet.get_latest_new_year_resolution_tweets(strText)
if Tweet.count > 0
Tweet.delete_all
end
Tweet.get_latest_new_year_resolution_tweets(strText)
@tweets = Tweet.order("twitter_created_at desc")
render 'index'
end
end
Index.html.erb (The view file)
<h1>Twitter connect</h1>
<form action="create" method="post">
<label for="keyword">Enter Keyword</label>
<input id="keyword" name="tweet[search]" size="30" type="text" />
<input type="submit" value="search" />
</br> <br>
</form>
</br></br>
<div id="container">
<% if (@tweets != nil && @tweets.count>0) then %>
<ul>
<% @tweets.each do |tweet| %>
<li class="<%=cycle('odd', '')%>">
<%= link_to tweet.from_user, "http://twitter.com/#{tweet.from_user}", :class => "username", :target => "_blank" %>
<div class="tweet_text_area">
<div class="tweet_text">
<%=raw display_content_with_links(tweet.text) %>
</div>
<div class="tweet_created_at">
<%= time_ago_in_words tweet.twitter_created_at %> ago
</div>
</div>
</li>
<% end %>
</ul>
<% end %>
</div>
The gemfile is as below
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3', '1.3.6',:group => :development
#gem 'ruby-mysql'
#gem 'mysql2'
group :production do
gem 'pg'
end
gem 'twitter', '4.6.2'
Upvotes: 0
Views: 385
Reputation: 446
Include below code in your application.rb or top of app/models/tweet.rb files
require 'twitter'
Ex:-
class Tweet < ActiveRecord::Base
require 'twitter'
....
...
end
Upvotes: 2