Note that there are some explanatory texts on larger screens.

plurals
  1. POIntegrating twitter with Ruby on Rails to display dynamic feeds
    primarykey
    data
    text
    <p>I have created a Ruby on Rails application and <strong>integrated twitter</strong>. But i am facing some issues while running it. I have an index page with a <strong>search box</strong> and a <strong>submit button</strong>. The application tries to search the keywords entered on twitter and then display it dynamically on the application page(the create.html.page for dynamically displaying tweets). So there is a "tweet controller" and two html.erb files. "Index.html.erb" , "create.html.erb" . And a model. The index method in controller is used to display feeds using a <strong>specific keyword</strong> (You can say like a <strong>static</strong> way). The create method is used to <strong>dynamically display the tweets</strong> based on the keyword entered in the index.html.erb. I wanted to avoid using form_for or form_tag so created a form using html form tag.</p> <p>When i try to execute the url "http:localhost:3000/tweets/create" it displays an error that <strong>symbol as array index</strong> When i try to execute the url "http:localhost:3000/tweets/index" , the page is not rendered. Kindly help. I m posting the controller, views below.</p> <p>Thanks, bhargav</p> <p><strong>"Index.html.erb"</strong></p> <pre><code>&lt;h1&gt;Tweets about New Year Resolution&lt;/h1&gt; &lt;form action="/create" method="post"&gt; &lt;label for="keyword"&gt;keyword&lt;/label&gt; &lt;input id="keyword" name="Enter keyword" size="30" type="text" /&gt; &lt;input type="submit" value="search" /&gt; &lt;/form&gt; &lt;% if (@tweets != nil) then %&gt; print "The tweets are " @tweets &lt;% else %&gt; print "No tweets for the keyword" &lt;% end % &gt; &lt;div id="container"&gt; &lt;ul&gt; &lt;% @tweets.each do |tweet| %&gt; &lt;li class="&lt;%=cycle('odd', '')%&gt;"&gt; &lt;%= link_to tweet.from_user, "http://twitter.com/#{tweet.from_user}", :class =&gt; "username", :target =&gt; "_blank" %&gt; &lt;div class="tweet_text_area"&gt; &lt;div class="tweet_text"&gt; &lt;%=raw display_content_with_links(tweet.text) %&gt; &lt;/div&gt; &lt;div class="tweet_created_at"&gt; &lt;%= time_ago_in_words tweet.twitter_created_at %&gt; ago &lt;/div&gt; &lt;/div&gt; &lt;/li&gt;`enter code here` &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p><strong>"create.html.erb"</strong></p> <pre><code>&lt;h1&gt;Keywords displayed&lt;/h1&gt; &lt;p&gt; A page to display tweets &lt;/p&gt; </code></pre> <p><strong>"tweets_controller.rb"</strong></p> <pre><code>class TweetsController &lt; ApplicationController def index #Get the tweets (records) from the model Ordered by 'twitter_created_at' descending if Tweet.count &gt; 0 Tweet.delete_all end Tweet.get_latest_new_year_resolution_tweets("India") @tweets = Tweet.order("twitter_created_at desc") end def create @tweets = (params [:tweet][:search]) Tweet.get_latest_new_year_resolution_tweets(@tweets) end end </code></pre> <p>The <strong>model</strong> *<em>tweet.rb</em>* contains the method get_latest_new_year_resolution_tweets . It is as below.</p> <pre><code>class Tweet &lt; ActiveRecord::Base #A method to grab latest tweets from Twitter def self.get_latest_new_year_resolution_tweets(keyword) #create a Twitter Search object search = Twitter::Search.new #grab recent 100 tweets which contain 'new year resolution' words, and loop each of them search.containing(keyword).result_type("recent").per_page(100).fetch.each do |tweet_results| #parsing the string 'created_at' to DateTime object twitter_created_at = DateTime.parse(tweet_results.created_at) #making sure we are not saving exact same tweet from a person again unless Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', DateTime.parse(tweet_results.created_at), tweet_results.from_user_id_str]) #finally creating the tweet record Tweet.create!({ :from_user =&gt; tweet_results.from_user, :from_user_id_str =&gt; tweet_results.from_user_id_str, :profile_image_url =&gt; tweet_results.profile_image_url, :text =&gt; tweet_results.text, :twitter_created_at =&gt; twitter_created_at }) end end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload