<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>{ enygmatic } &#187; Ruby</title>
	<atom:link href="http://blog.enygmatic.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.enygmatic.com</link>
	<description>mobilis in mobili</description>
	<lastBuildDate>Thu, 26 Jan 2012 17:46:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>RJS Magic</title>
		<link>http://blog.enygmatic.com/2009/06/27/rjs-magic/</link>
		<comments>http://blog.enygmatic.com/2009/06/27/rjs-magic/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 06:09:17 +0000</pubDate>
		<dc:creator>enygmatic</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[RJS]]></category>

		<guid isPermaLink="false">http://blog.enygmatic.com/?p=334</guid>
		<description><![CDATA[Recently, while working on a small application on Rails, I learned firsthand how easy and powerful RJS can be. I was amazed at how simple it is to save attributes to the data base using Ajax and then notify the user about successful operations or errors. Here is a small example that should show you [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, while working on a small application on Rails, I learned firsthand how easy and powerful RJS can be. I was amazed at how simple it is to save attributes to the data base using Ajax and then notify the user about successful operations or errors. Here is a small example that should show you how simple this is.<span id="more-334"></span></p>
<h3>The Model</h3>
<p>For the purpose of this short example, I’ll consider a table with only one field other than ‘id’. Here is the migration for it:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreatePersons <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:persons</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
     t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>, <span style="color:#ff3333; font-weight:bold;">:null</span><span style="color:#006600; font-weight:bold;">=&gt;</span>false
    <span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
 <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
  drop_table <span style="color:#ff3333; font-weight:bold;">:persons</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>The View</h3>
<p>In the view on the ‘index’ page the table is rendered as:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;h1&gt;List of Persons&lt;/h1&gt;
&lt;div id=&quot;error&quot;  style=&quot;display:none;&quot; &gt;&amp;nbsp;&lt;/div&gt;
&lt;div id=&quot;notice”  style=&quot;display:none;&quot; &gt;&amp;amp;nbsp;&lt;/div&gt;
&lt;table id=&quot;person_list&quot;&gt;
  &lt;tr&gt;
    &lt;td width=&quot;300px&quot;&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
  &lt;/tr&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ‘person’, <span style="color:#ff3333; font-weight:bold;">:collection</span><span style="color:#006600; font-weight:bold;">=&gt;</span>@persons <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/table&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span>=<span style="color:#5A0A0A; font-weight:bold;">link_to_function</span> <span style="color:#996600;">'New Person’, &quot;Element.show('</span>new_person<span style="color:#996600;">')&quot;%&gt;</span>
&lt;div id=&quot;new_person&quot; style=&quot;display:none;&quot;&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;">form_remote_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'create'</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
<span style="color:#ff3333; font-weight:bold;">:html</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:id <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'person_form'</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;strong&gt;Name:&lt;/strong&gt;&lt;br/&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= text_field <span style="color:#996600;">&quot;person&quot;</span>, <span style="color:#996600;">&quot;name&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span>= submit_tag <span style="color:#996600;">&quot;Save&quot;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span>end<span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/div&gt;</pre></div></div>

<p>The partial contains the following:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;tr&gt;
  &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= person.<span style="color:#9900CC;">name</span><span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
&lt;/tr&gt;</pre></div></div>

<p>Basically the controller returns a list of all persons and this is looped and displayed through the partial. There is also a hidden form that submits a new name to be added to the database through Ajax. The form is made visible on clicking the “New Person” link and submits to the “create” method in the controller.</p>
<h3>The Controller</h3>
<p>The “create” method does the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> create
 <span style="color:#0066ff; font-weight:bold;">@person</span> = Person.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:person</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@person</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span>
  <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'A Person was added successfully to the database'</span>
 <span style="color:#9966CC; font-weight:bold;">else</span>
  <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
   page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">show</span>
   page.<span style="color:#5A0A0A; font-weight:bold;">insert_html</span> <span style="color:#ff3333; font-weight:bold;">:bottom</span>, <span style="color:#ff3333; font-weight:bold;">:error</span>, <span style="color:#5A0A0A; font-weight:bold;">error_messages_for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:person</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
 <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Here,  first the person object is created from the request parameters. Then the values are added to the database. If they are successfully added, then a message is added to the flash. If there are any errors, then we extract any errors and display them on the page. This is done via the really cool feature of Rails called RJS. RJS lets us generate JavaScript on the fly by only writing ruby code. In this example, line 7 selects the error div and makes it visible. Line 8 inserts the error messages into the error div. The resulting JavaScript is executed on the page. The end result – the errors are displayed in the error div in case of errors.</p>
<h3>RJS Magic</h3>
<p>Sweet? Well this only the tip of the iceberg. I’ll take things a wee bit further and show you what happens when we add a little RJS template called “create.rjs”. The code for this template is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Hide Form &amp;amp; Error</span>
page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:person_name</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">text</span>=<span style="color:#996600;">&quot;&quot;</span>
page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:new_person</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">hide</span>
page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">hide</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Show Notice and add disappearing effect</span>
page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">show</span>
page.<span style="color:#5A0A0A; font-weight:bold;">insert_html</span> <span style="color:#ff3333; font-weight:bold;">:bottom</span>, <span style="color:#ff3333; font-weight:bold;">:notice</span>, <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span>
page.<span style="color:#9900CC;">delay</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>page<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">visual_effect</span> <span style="color:#ff3333; font-weight:bold;">:fade</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Add row to table</span>
page.<span style="color:#5A0A0A; font-weight:bold;">insert_html</span> <span style="color:#ff3333; font-weight:bold;">:bottom</span>, <span style="color:#ff3333; font-weight:bold;">:person_list</span>, <span style="color:#ff3333; font-weight:bold;">:partial</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;">'person'</span>, <span style="color:#ff3333; font-weight:bold;">:object</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;@person</pre></div></div>

<p>Well here is how it works. The first three lines basically set the text field on the form to blank, hide the form and any visible errors (You can even overwrite the error div contents so that they contain no errors on successful saves). Next we show the notice div and add the notice message to the div. Now here is the really cool part, the next line adds a Fade effect via the script.aculo.us animation framework and sets the delay to 5 seconds for this effect. This shows the notice message for 5 seconds after which the message fades out. What’s really cool about this, is that it’s just one line of Ruby code and no messy JavaScript in sight (Of course the last line updates the table with the newly added person entry).</p>
<p>I think this example goes a long way in showing how productive Rails is. And this is just a fraction of what you could do with RJS and Rails, while developing applications that use a lot of Ajax.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.enygmatic.com/2009/06/27/rjs-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Financing Rails</title>
		<link>http://blog.enygmatic.com/2008/09/28/financing-rails/</link>
		<comments>http://blog.enygmatic.com/2008/09/28/financing-rails/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 07:01:09 +0000</pubDate>
		<dc:creator>enygmatic</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.enygmatic.com/?p=130</guid>
		<description><![CDATA[I have been working on this small Rails based dashboard (code named : Fx Dashboard) the past few months. The basic idea that I had was to create some kind of a dashboard where I could collate all relevant financial information and news. The application has turned out pretty well, and I finally managed to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://enygmatic.com/blog/wp-content/uploads/2008/09/fx-dashboard-small.jpg" alt="FX Dashboard" /></p>
<p>I have been working on this small Rails based dashboard (code named : Fx Dashboard) the past few months. The basic idea that I had was to create some kind of a dashboard where I could collate all relevant financial information and news. The application has turned out pretty well, and I finally managed to put it online yesterday. The application currently displays a 15 minute delayed feed of major market indices, delayed feeds of sub-indices on the BSE and the NSE, commodity prices from MCX, news feeds from IBN, Moneycontrol, BBC World and Reuters and the latest foreign exchange rates from FEDAI (see above screen shot). The only thing I haven&#8217;t been able to integrate as yet are the RBI rates.</p>
<p>Building this application was a very learning experience.I learnt how to use Flex and integrate it with Rails. Also learnt what &#8220;screen scraping&#8221; is, and how to do it using Rails. As of now, this application is for personal use (might change in the future). Basically I want to test run it on a live server, see bandwidth usage etc. Lets see how it turns out.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.enygmatic.com/2008/09/28/financing-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Labs goes live !!</title>
		<link>http://blog.enygmatic.com/2008/03/10/labs-goes-live/</link>
		<comments>http://blog.enygmatic.com/2008/03/10/labs-goes-live/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 16:34:43 +0000</pubDate>
		<dc:creator>enygmatic</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Websites]]></category>

		<guid isPermaLink="false">http://blog.enygmatic.com/?p=78</guid>
		<description><![CDATA[After a long delay, Labs is finally online. You can check out complete experiments and projects as well as read some articles that I have written. The Labs section will primarily showcase some of my small open source based projects as well articles and papers. The very first project available for download is Loupe, the [...]]]></description>
			<content:encoded><![CDATA[<p>After a long delay, <a href="http://labs.enygmatic.com" title="Labs" target="_blank">Labs </a>is finally online. You can check out complete experiments and projects as well as read some articles that I have written. The Labs section will primarily showcase some of my small open source based projects as well articles and papers.</p>
<p>The very first project available for download is Loupe, the rails+flickr image gallery. This is the same gallery that I run my photography site on. So please visit the site, and download and try the scripts. Any feedback would of course be welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.enygmatic.com/2008/03/10/labs-goes-live/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loupe &#8211; Flickerize your website</title>
		<link>http://blog.enygmatic.com/2007/10/03/loupe-flickerize-your-website/</link>
		<comments>http://blog.enygmatic.com/2007/10/03/loupe-flickerize-your-website/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 08:20:57 +0000</pubDate>
		<dc:creator>enygmatic</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.enygmatic.com/?p=29</guid>
		<description><![CDATA[About three months ago started with an idea of using Flickr&#8217;s public API to pull my Flickr pictures on to a sub section of my website. I codenamed this project &#8220;Loupe&#8221;. While I was done with most of coding in under a week, B School kept me way too busy to do anything about Loupe. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://enygmatic.com/blog/wp-content/uploads/2007/10/screenshot.png" title="Loupe" alt="Loupe" align="left" />About three months ago started with an idea of using Flickr&#8217;s public API to pull my Flickr pictures on to a sub section of my website. I codenamed this project &#8220;Loupe&#8221;. While I was done with most of coding in under a week, B School kept me way too busy to do anything about Loupe. As such the project was literally hibernating. Now with the first trimester done, and with finally some time on my hand I finally managed to put up Loupe on the web !</p>
<p>What Loupe basically does is pick all the pictures from a user&#8217;s public photostream on flickr and display it on a website. All the flickr niceties like tags, sets etc can be accessed. In addition to this there is also a tag based search feature as well as nifty about page to put material on. ( Mine still needs some work, right now it&#8217;s just some random text I picked up from another photography site ).</p>
<p>Loupe uses the <a href="http://rubyforge.org/projects/rflickr/" title="rFlickr" target="_blank">rflickr</a> library to access the <a href="http://www.flickr.com" title="Flickr" target="_blank">Flickr</a> API. It is built using the excellent web framework for <a href="http://www.ruby-lang.org/" title="Ruby Language" target="_blank">Ruby</a> called <a href="http://www.rubyonrails.org/" title="Ruby on Rails" target="_blank">Rails</a>. It also uses the <a href="http://mootools.net/" title="mootools" target="_blank">mootools</a> and <a href="http://www.digitalia.be/software/slimbox" title="Slimbox" target="_blank">slim box</a> javascript libraries for displaying images. Do give Loupe a look at my <a href="http://photos.enygmatic.com" title="Photos" target="_blank">photography </a>section and let me know how it is. I will also be writing a series of articles on rails and flickr integration in the coming weeks, so do watch this space for more.</p>
<p><strong>Technorati Tags:</strong> <a href="http://www.technorati.com/tag/Photography" rel="tag">Photography</a>,  <a href="http://www.technorati.com/tag/Loupe" rel="tag">Loupe</a>,  <a href="http://www.technorati.com/tag/Rails" rel="tag">Rails</a>,  <a href="http://www.technorati.com/tag/Flickr" rel="tag">Flickr</a>,  <a href="http://www.technorati.com/tag/Ruby" rel="tag">Ruby</a>,  <a href="http://www.technorati.com/tag/rflickr" rel="tag">rflickr</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.enygmatic.com/2007/10/03/loupe-flickerize-your-website/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

