{"id":7022,"date":"2014-05-17T00:21:19","date_gmt":"2014-05-17T00:21:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/rubyrails-how-to-access-create-action-without-leaving-index-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:21:19","modified_gmt":"2014-05-17T00:21:19","slug":"rubyrails-how-to-access-create-action-without-leaving-index-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/rubyrails-how-to-access-create-action-without-leaving-index-collection-of-common-programming-errors\/","title":{"rendered":"RubyRails: How to access create action, without leaving index-Collection of common programming errors"},"content":{"rendered":"<p>You&#8217;ve got two big pieces of this:<\/p>\n<ul>\n<li>Listening to the streaming API and creating objects<\/li>\n<li>Updating the user&#8217;s view as new objects are created<\/li>\n<\/ul>\n<p>The second bit can be done by some simple javascript polling, as in @AustinMullins&#8217;s answer.<\/p>\n<p>The first bit should <em>not<\/em> be done in a controller &#8211; they are for responding to requests and deviating from that kind of job may end up with unexpected behavior or performance issues.<\/p>\n<p>For example, I found that on a site that&#8217;s running on Phusion Passenger, the server would create a thread to handle a request, then <em>kill it<\/em> after a certain amount of time if it didn&#8217;t finish by itself, which of course it would not if the controller starts listening to a neverending input stream.<\/p>\n<p>Instead, you should get a separate script that you can start from the command line. Here&#8217;s an example similar to what I&#8217;m using:<\/p>\n<p><strong>script\/tracker.rb<\/strong><\/p>\n<pre><code>#!\/usr\/bin\/env ruby\nENV[\"RAILS_ENV\"] ||= \"production\"\nrequire File.dirname(__FILE__) + \"\/..\/config\/environment\"\nTweetStream.configure do |config|\n  config.consumer_key = TWITTER_COMSUMER_KEY\n  config.consumer_secret = TWITTER_CONSUMER_SECRET\n  config.oauth_token = TWITTER_OATH_TOKEN\n  config.oauth_token_secret = TWITTER_OATH_TOKEN_SECRET\nend\n\nclient = TweetStream::Client.new\nclient.track(*Keyword.pluck(:name)).each do |status|\n  Tweet.create(status.text)\nend\n<\/code><\/pre>\n<p><strong>script\/tracker_ctl<\/strong><\/p>\n<pre><code>#!\/usr\/bin\/env ruby\nrequire 'rubygems'\nrequire 'daemons'\nDaemon.new('tracker.rb')\n<\/code><\/pre>\n<p>Now, you could ssh into your server and run <code>script\/tracker_ctl start<\/code> or <code>script\/tracker_ctl stop<\/code>. But what you probably want to do is make a controller action issue those commands instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve got two big pieces of this: Listening to the streaming API and creating objects Updating the user&#8217;s view as new objects are created The second bit can be done by some simple javascript polling, as in @AustinMullins&#8217;s answer. The first bit should not be done in a controller &#8211; they are for responding to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7022","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7022"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7022\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}