<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Who Needs Actions]]></title>
  <link href="http://mrjamesriley.github.com/atom.xml" rel="self"/>
  <link href="http://mrjamesriley.github.com/"/>
  <updated>2012-05-09T08:51:53+01:00</updated>
  <id>http://mrjamesriley.github.com/</id>
  <author>
    <name><![CDATA[James Riley]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Simple Google Maps Example with Coffeescript and jQuery]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/05/08/simple-maps-example-with-coffeescript-and-jquery/"/>
    <updated>2012-05-08T07:51:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/05/08/simple-maps-example-with-coffeescript-and-jquery</id>
    <content type="html"><![CDATA[<p>I&#8217;m working with the latest version of the Google Maps API (V3) and the demo in the <a href="https://developers.google.com/maps/documentation/javascript/tutorial">developers guide</a> guide screams out for a dose of Coffeescript and jQuery. The three Javascript requirements here are:</p>

<ol>
<li>Include the Google Maps API</li>
<li>Define the <code>initialize()</code> function</li>
<li>Call the <code>initialize()</code> function <code>onload</code> (one the page has finished loading)</li>
</ol>


<p>Here&#8217;s a before and after for the Coffeescript/jQuery makeover:</p>

<!-- more -->




<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'>  <span class="c1">// Javascript</span>
</span><span class='line'>  <span class="kd">function</span> <span class="nx">initialize</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">myOptions</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">center</span><span class="o">:</span> <span class="k">new</span> <span class="nx">google</span><span class="p">.</span><span class="nx">maps</span><span class="p">.</span><span class="nx">LatLng</span><span class="p">(</span><span class="o">-</span><span class="mf">34.397</span><span class="p">,</span> <span class="mf">150.644</span><span class="p">),</span>
</span><span class='line'>          <span class="nx">zoom</span><span class="o">:</span> <span class="mi">8</span><span class="p">,</span>
</span><span class='line'>          <span class="nx">mapTypeId</span><span class="o">:</span> <span class="nx">google</span><span class="p">.</span><span class="nx">maps</span><span class="p">.</span><span class="nx">MapTypeId</span><span class="p">.</span><span class="nx">ROADMAP</span>
</span><span class='line'>    <span class="p">};</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">map</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">google</span><span class="p">.</span><span class="nx">maps</span><span class="p">.</span><span class="nx">Map</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">&quot;map_canvas&quot;</span><span class="p">),</span>
</span><span class='line'>        <span class="nx">myOptions</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// Coffeescript</span>
</span><span class='line'>  <span class="nx">initialize</span> <span class="o">=</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">myOptions</span> <span class="o">=</span>
</span><span class='line'>      <span class="nx">center</span><span class="o">:</span> <span class="k">new</span> <span class="nx">google</span><span class="p">.</span><span class="nx">maps</span><span class="p">.</span><span class="nx">LatLng</span> <span class="o">-</span><span class="mf">34.397</span><span class="p">,</span> <span class="mf">150.644</span>
</span><span class='line'>      <span class="nx">zoom</span><span class="o">:</span> <span class="mi">8</span><span class="p">,</span>
</span><span class='line'>      <span class="nx">mapTypeId</span><span class="o">:</span> <span class="nx">google</span><span class="p">.</span><span class="nx">maps</span><span class="p">.</span><span class="nx">MapTypeId</span><span class="p">.</span><span class="nx">ROADMAP</span>
</span><span class='line'>    <span class="nx">map</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">google</span><span class="p">.</span><span class="nx">maps</span><span class="p">.</span><span class="nx">Map</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#map_canvas&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">myOptions</span>
</span></code></pre></td></tr></table></div></figure>


<p>Variables scoped locally by default, curly braces made redundant, semi-colons unecessary, readability increased and number of lines reduced. Of course, this only scratches the surface of what <a href="http://coffeescript.org">Coffeescript offers</a>. Notice also the use of jQuery to select the &#8216;map_canvas&#8217; div, remembering that the selector returns an array and we want the actual DOM element.</p>

<h2>Body Onload</h2>

<p>Adding an <code>onload</code> to the <code>body</code> tag can be a pain, especially if you want it conditional, it&#8217;s likely you&#8217;re layout is shared amongst other pages. Thankfully we can take advantage of how jQuery can run once the page has loaded, moving this out of the body tag and into your external Javascript file like so:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'>  <span class="c1">// this</span>
</span><span class='line'>  <span class="o">&lt;</span><span class="nx">body</span> <span class="nx">onload</span><span class="o">=</span><span class="s2">&quot;initialize()&quot;</span><span class="o">&gt;</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// becomes</span>
</span><span class='line'>  <span class="nx">$</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="nx">initialize</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Including the Maps API</h2>

<p>As I mentioned it above, it&#8217;d be rude not to elaborate - here&#8217;s a quick Rails example of how it can be done. We don&#8217;t want to simply place a <code>javascript_include_tag</code> in our layout if the only want the map on a single page. A clean method here is to pass the javascript include in a call to <code>content_for</code> which is then rendered in your layout with use of the yield method. Hopefully this makes it clear:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="c1"># application layout</span>
</span><span class='line'>  <span class="k">yield</span> <span class="ss">:js</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># the view</span>
</span><span class='line'>  <span class="o">-</span> <span class="n">content_for</span> <span class="ss">:js</span> <span class="k">do</span>
</span><span class='line'>    <span class="o">=</span> <span class="n">javascript_include_tag</span> <span class="s1">&#39;http://maps.googleapis.com/maps/api/js?key=API_KEY=false&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>:js</code> symbol can be anything, it&#8217;s simply an identifier but it helps to keep it somewhat generic so the yield can be used by other views.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Easy toggle fade with fadeToggle()]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/05/03/easy-toggle-fade-with-fadetoggle/"/>
    <updated>2012-05-03T17:52:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/05/03/easy-toggle-fade-with-fadetoggle</id>
    <content type="html"><![CDATA[<p>The jQuery <code>toggle()</code> function is great for easily switching an element to hidden or not. Today I was <a href="http://www.olaportuguese.com/interactive/visual-dictionary/face">working on something</a> where I wanted to toggle with a fade out, and I stumbled across the <a href="http://www.olaportuguese.com/interactive/visual-dictionary/face">fadeToggle()</a> function. Simple and easy to use, typical of jQuery.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Pow Server for Rails Applications]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/05/02/pow-server-for-rails-applications/"/>
    <updated>2012-05-02T19:44:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/05/02/pow-server-for-rails-applications</id>
    <content type="html"><![CDATA[<p>Many Rails developers have many apps and think nothing of switching between them as necessary with the trusty <code>rails server</code> command. Each morning cosists of the familiar routine: open terminal, start auto-running tests, and boot the dev server. The <a href="http://pow.cx">Pow</a> server makes this final step unecessary.</p>

<p>Pow is a super simple to set-up solution for serving Rails apps on your dev machine. By simple, we&#8217;re talking a single command, and by Rails, we&#8217;re talking Rack apps which extends to any Rack solution you muster up. The <a href="http://get.pow.cx/media/screencast.mov">Pow Screencast</a> will get you up to speed in no time. Watch the video, symlink your Rack apps and hit <code>{folder}.dev</code> in your browser, where <code>{folder}</code> is the name of the newly created symlink that should be within <code>~/.pow/</code> by default.</p>

<p><em>Tip</em>: As mentioned above, by default Pow is set up to use <code>.dev</code>, which Google Chrome doesn&#8217;t recognise and will &#8216;helpfully&#8217; treat it as a search query. The solution here? End the call with a forward slash <code>/</code> e.g <code>hamster.dev/</code>. Finally, a solution for using <code>debugger</code> with a Rails app served through Pow can be found <a href="https://gist.github.com/1098830">here</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby Koans Scoring Project]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/05/01/ruby-koans-scoring-project/"/>
    <updated>2012-05-01T17:05:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/05/01/ruby-koans-scoring-project</id>
    <content type="html"><![CDATA[<p><a href="http://rubykoans.com">Ruby Koans</a> has sat on my todo list for almost as long as I&#8217;ve been a Ruby developer and this week I&#8217;ve finally taken it upon myself to tackle it. Expectedly, there&#8217;s little new to be expected for anyone who has served their time as a developer, but there&#8217;s plenty of helpful tricks and Ruby intricacies to be reminded of. I&#8217;ve enjoyed looking at how others have <a href="http://stackoverflow.com/questions/4749973/ruby-greed-koan-how-can-i-improve-my-if-then-soup">tackled the problem</a>, so assuming you&#8217;ve already given the question a good attempt, here&#8217;s my solution:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">score</span><span class="p">(</span><span class="n">dice</span><span class="p">)</span>
</span><span class='line'>  <span class="n">score</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'>  <span class="mi">1</span><span class="o">.</span><span class="n">upto</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">num</span><span class="o">|</span>
</span><span class='line'>    <span class="n">amount</span> <span class="o">=</span> <span class="n">dice</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">amount</span> <span class="o">&gt;=</span> <span class="mi">3</span>
</span><span class='line'>      <span class="n">score</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">?</span> <span class="mi">1000</span> <span class="p">:</span> <span class="n">num</span> <span class="o">*</span> <span class="mi">100</span>
</span><span class='line'>      <span class="n">amount</span> <span class="o">-=</span> <span class="mi">3</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">score</span> <span class="o">+=</span> <span class="mi">100</span> <span class="o">*</span> <span class="n">amount</span> <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">1</span>
</span><span class='line'>    <span class="n">score</span> <span class="o">+=</span> <span class="mi">50</span> <span class="o">*</span> <span class="n">amount</span> <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">5</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">score</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Settle for excellence]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/04/30/settle-for-excellence/"/>
    <updated>2012-04-30T17:48:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/04/30/settle-for-excellence</id>
    <content type="html"><![CDATA[<p>Today was spent away from my desk, but I was reminded of a great quote by George S. Patton, an American officer during World War 2. Now, I can&#8217;t vouch for his programming chops but the quote very much applies to web projects:</p>

<blockquote><p>&#8220;A good plan violently executed today is better than the perfect plan tomorrow.&#8221;</p></blockquote>

<p>Start now.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Haml helpers Succeed and Preceed]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/04/27/haml-helpers-succeed-and-preceed/"/>
    <updated>2012-04-27T16:59:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/04/27/haml-helpers-succeed-and-preceed</id>
    <content type="html"><![CDATA[<p>With Haml, placing a full-stop or comma immediately after a link, for example, can be confusing. Sure, we have the whitespace removal functionaliy removal that gives us the ability to place <code>&lt;</code> or <code>&gt;</code> to remove the space before before and after a tag. The issue is, it&#8217;s ugly, unclear and I&#8217;m come across instances where it simply doesn&#8217;t do as advertised.</p>

<p>The <a href="http://haml-lang.com/docs/yardoc/Haml/Helpers.html">haml helpers</a> page helpfully offers the <code>succeed</code> and <code>preceed</code> helpers that can be used as follows:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">=</span> <span class="n">succeed</span> <span class="s1">&#39;.&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="o">%</span><span class="n">a</span><span class="p">{</span> <span class="n">href</span><span class="p">:</span> <span class="s1">&#39;example.com&#39;</span> <span class="p">}</span> <span class="no">Example</span>
</span><span class='line'><span class="c1"># =&gt; &lt;a href=&quot;example.com&quot;&gt;Example&lt;/a&gt;.</span>
</span></code></pre></td></tr></table></div></figure>


<p>If it isn&#8217;t clear, the succeed helper takes a block which it&#8217;ll evaluate and place the passed string directly after, without white space. In this case, a full-stop is placed directly after the generated link. <code>preceed</code> works the same, but places the given string before the output.</p>

<p>Writing out my own example was motivated purely by my strong preference for the Ruby 1.9 hash syntax.
As far as programming tidbits go, this is a good candidate. Small and tasty. Till Monday!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Geolocation for Amazon Link Localising]]></title>
    <link href="http://mrjamesriley.github.com/blog/2012/04/26/geolocation-for-amazon-link-localising/"/>
    <updated>2012-04-26T17:57:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2012/04/26/geolocation-for-amazon-link-localising</id>
    <content type="html"><![CDATA[<p>This post starts a 30 days experiment. I&#8217;m not alone in starting a blog with every intention of keeping consistent, only to find weeks fly past without so much as a grunt. So for 30 days, this blog takes on a new format: each day I will end my &#8216;working day&#8217; with something I learnt that day. However small, however insignificant, someone somewhere will stumble where I fell or ponder thoughts I&#8217;ve processed.</p>

<p>A purpose of today&#8217;s post is to make you aware of <a href="http://freegeoip.net/static/index.html">FreeGeoIP</a>, a solution for those wanting to look up a visitors country by IP. In my case, I&#8217;ve started a site where I hope to <a href="http://www.olaportuguese">teach portuguese</a> and wanted a way to link to a mentioned book to the appropriate Amazon website.</p>

<!--more-->


<p>The FreeGEOIP websites offers a simple web service that receives your chosen format (csv, xml or json) and an ipaddress, quickly hitting you back with a bunch of location data. The only downside here, is that the service is limited to 1000 hits a day, so if you have a large site, you could consider setting up a local instance yourself as the project is open source. Using Rails 3, and the great <code>HTTParty</code> gem, which makes consuming web services a walk in the park, using the service couldn&#8217;t be simpler:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">ip_address</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">remote_ip</span>
</span><span class='line'><span class="n">res</span> <span class="o">=</span> <span class="no">HTTParty</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;http://freegeoip.net/json/</span><span class="si">#{</span><span class="n">ip_address</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
</span><span class='line'><span class="no">JSON</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">body</span><span class="p">)</span>
</span><span class='line'><span class="c1"># =&gt; { &#39;country_name&#39; =&gt; &#39;United Kingdom&#39;, &#39;country_code&#39; =&gt; &#39;GB&#39; ... }</span>
</span></code></pre></td></tr></table></div></figure>


<p>The solution I considered was placing a method in the <code>ApplicationController</code>, with a <code>before_filter</code> set to perform an IP lookup and save the country code to a cookie, unless such a cookie for the visitor already existed. From here, I was to have a helper that would show a default Amazon link unless a matching country cookie was set. The helper would take an Amazon product ID and be used as the second arguement of a call to a <code>link_to</code> call:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="c1"># If country cookie is set, show relevant Amazon link or default to UK</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">amazon_link</span><span class="p">(</span><span class="n">product</span><span class="p">)</span>
</span><span class='line'>    <span class="n">countries</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>      <span class="s1">&#39;GB&#39;</span>  <span class="o">=&gt;</span> <span class="p">{</span> <span class="n">tld</span><span class="p">:</span> <span class="s1">&#39;co.uk&#39;</span><span class="p">,</span> <span class="nb">id</span><span class="p">:</span> <span class="s1">&#39;{UK_ASSOCIATES_ID}&#39;</span> <span class="p">},</span>
</span><span class='line'>      <span class="s1">&#39;US&#39;</span> <span class="o">=&gt;</span>  <span class="p">{</span> <span class="n">tld</span><span class="p">:</span> <span class="s1">&#39;com&#39;</span><span class="p">,</span>   <span class="nb">id</span><span class="p">:</span> <span class="s1">&#39;{US_ASSOCIATES_ID}&#39;</span> <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">if</span> <span class="n">cookies</span><span class="o">[</span><span class="s1">&#39;country&#39;</span><span class="o">]</span> <span class="o">&amp;&amp;</span> <span class="n">countries</span><span class="o">.</span><span class="n">has_key?</span><span class="p">(</span><span class="n">cookies</span><span class="o">[</span><span class="s1">&#39;country&#39;</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>      <span class="n">amazon</span> <span class="o">=</span> <span class="n">countries</span><span class="o">[</span><span class="n">cookies</span><span class="o">[</span><span class="s1">&#39;country&#39;</span><span class="o">]]</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>      <span class="n">amazon</span> <span class="o">=</span> <span class="n">countries</span><span class="o">[</span><span class="s1">&#39;GB&#39;</span><span class="o">]</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="s2">&quot;http://www.amazon.</span><span class="si">#{</span><span class="n">amazon</span><span class="o">[</span><span class="ss">:tld</span><span class="o">]</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">product</span><span class="si">}</span><span class="s2">/?tag=</span><span class="si">#{</span><span class="n">amazon</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the end I scrapped the idea, mainly because I don&#8217;t like the overhead of hitting an API for every new user, but also for privacy issues. It&#8217;s a topic that&#8217;s difficult to find information on, but what is and what isn&#8217;t allowed regarding a users IP? And what alternatives are there to obtaining the country of a site visitor?</p>

<p>Well, there&#8217;s HTML5 Geolocation, but this requires rightfully requires permission and could be considered overkill for this simple scenario. There are also services like <a href="https://www.cloudflare.com/">Cloudfare</a>, that stand between your domain registar and your web application, passing along a header with country code, among many other benefits.</p>

<p>Ultimately, I feel it&#8217;s a flaw with Amazon&#8217;s Associates program - not only do you have to sign up for each of the numerous websites indepedently, an automatic redirection system could be offered that would greatly simplify the process for publishers, and encourage it&#8217;s use, thus increasing their income. What gives?</p>

<p>Well that was more wordy than I expected, perhaps that&#8217;s why the post rate drops off everytime. Hah, check back for more tomorrow! Have you a good solution for simple geo-location? <a href="https://twitter.com/#!/mrjamesriley">Hit me up on Twitter</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Easy HTML Table Pagination and Sorting with DataTables]]></title>
    <link href="http://mrjamesriley.github.com/blog/2011/12/23/easy-jquery-table-pagination/"/>
    <updated>2011-12-23T23:53:00+00:00</updated>
    <id>http://mrjamesriley.github.com/blog/2011/12/23/easy-jquery-table-pagination</id>
    <content type="html"><![CDATA[<p><a href="http://datatables.net">DataTables</a> is a powerful jQuery plugin that easily turns a standard HTML table into one that is sortable,
paginatable, searchable and
highly customisable. Having used the plugin on multiple occasions over the past few months, I deem it a huge time saver and if it&#8217;s
interactive tables you&#8217;re after – I wouldn&#8217;t look elsewhere. The
<a href="http://datatables.net/release-datatables/examples/basic_init/zero_config.html">basic example usage</a> makes its immediate power clear and there&#8217;s a wealth of options, an API and plenty of examples to satisfy most cases. Here&#8217;s a few tips on how to handle a few of the requirements I
commonly had. I love and use CoffeeScript but for your convenience, the examples below make use of the compiled Javascript,
simplified in places.</p>

<!--more-->


<h3>Custom pagination controls and info</h3>

<p>Via the sDom property on DataTable initialisation, it&#8217;s possible to customise the positioning of the pagination information and controls.
Aside from finding the resulting code hard to read, I also wanted more flexibility. First up, the pagination info (eg. Showing 1-10 of 25).
Upon DataTables creation, you can pass in a function for the &#8216;fnInfoCallback&#8217; which is triggered when the related status changes,
as a result of a page change or filter being applied. Here I simply form a string using the available parameters then update the text for the element with the id &#8216;#paginationInfo&#8217; on the page.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="s2">&quot;fnInfoCallback&quot;</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">oSettings</span><span class="p">,</span> <span class="nx">iStart</span><span class="p">,</span> <span class="nx">iEnd</span><span class="p">,</span> <span class="nx">iMax</span><span class="p">,</span> <span class="nx">iTotal</span><span class="p">,</span> <span class="nx">sPre</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">info</span> <span class="o">=</span> <span class="s2">&quot;Showing &quot;</span> <span class="o">+</span> <span class="nx">iStart</span> <span class="o">+</span> <span class="s2">&quot;-&quot;</span> <span class="o">+</span> <span class="nx">iEnd</span> <span class="o">+</span> <span class="s2">&quot; of &quot;</span> <span class="o">+</span> <span class="nx">iTotal</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">(</span><span class="err">&#39;#</span><span class="nx">paginationInfo</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">info</span><span class="p">);</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">info</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Next I wanted the freedom to position my &#8216;next&#8217; and &#8216;previous&#8217; pagination buttons anywhere, having assigned the DataTable initialisation to a variable (eg. <code>userList = $('table#userList').dataTables()</code>), making use of the public API,
specifically the fnPageChange function of the plugin makes this easy&#8230;</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#nextPage&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">userList</span><span class="p">.</span><span class="nx">fnPageChange</span><span class="p">(</span><span class="s1">&#39;next&#39;</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#prevPage&#39;</span><span class="p">).</span><span class="nx">click</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">userList</span><span class="p">.</span><span class="nx">fnPageChange</span><span class="p">(</span><span class="s1">&#39;previous&#39;</span><span class="p">);</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Multi filters and being able to clear all</h3>

<p>The DataTables <a href="http://datatables.net/api">API</a> makes filtering pretty simple with use of the <code>fnFilter</code> function, passing a string to filter
on and optionally, a specific column to limit filtering to. With multiple columns having filters applied, the easy way to clear all filters
is to simply call <code>fnFilter('')</code> - with an empty string and no column int arguement.</p>

<p>For the search field (which filters the table based on the entered text), I&#8217;m using the search input, part of HTML5
<code>&lt;input id="players-search" placeholder="Search by name..." type="search"&gt;</code> hooked up to filter the table on keyup.
If you&#8217;re using Webkit, you&#8217;ll see the search box includes a button on the right to clear the contents of the text field.
To have the table filter cleared when  clicking the &#8216;X&#8217;: set a &#8216;click&#8217; event on the search input which then takes its value
and filters accordingly. In this case, the value will now be blank after having clicked the &#8216;X&#8217; which effectively clears filtering on the table.</p>

<h3>Using images according to the row data</h3>

<p>It&#8217;s not long untill you&#8217;ll find yourself wanting to add buttons or images to the table, whether it be a users avatar,
or perhaps a &#8216;Buy&#8217; button for each product in the table. The <code>fnRowCallback</code> allows you to modify each row on draw and it&#8217;s
here you have access to the data for each column and able to modify the output. Say you have a table of forum users with
the following data, what if you wanted to show an image as a label for those who are admins?</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;tr&gt;</span>
</span><span class='line'>  <span class="nt">&lt;td&gt;</span>John<span class="nt">&lt;/td&gt;</span>
</span><span class='line'>  <span class="nt">&lt;td&gt;</span>Admin<span class="nt">&lt;/td&gt;</span>
</span><span class='line'>  <span class="nt">&lt;td&gt;</span>London<span class="nt">&lt;/td&gt;</span>
</span><span class='line'><span class="nt">&lt;/tr&gt;</span>
</span><span class='line'><span class="nt">&lt;tr&gt;</span>
</span><span class='line'>  <span class="nt">&lt;td&gt;</span>Paul<span class="nt">&lt;/td&gt;</span>
</span><span class='line'>  <span class="nt">&lt;td&gt;</span>Moderator<span class="nt">&lt;/td&gt;</span>
</span><span class='line'>  <span class="nt">&lt;td&gt;</span>New York<span class="nt">&lt;/td&gt;</span>
</span><span class='line'><span class="nt">&lt;/tr&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>When creating the DataTable, use the &#8216;aoColumns&#8217; table (or the <code>aoColumnDefs</code> alternative) to give classes to the columns to
allow us to easily target each:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="s1">&#39;aoColumns&#39;</span><span class="o">:</span> <span class="p">[</span>
</span><span class='line'>  <span class="p">{</span> <span class="s2">&quot;sClass&quot;</span><span class="o">:</span> <span class="s2">&quot;name&quot;</span><span class="p">,</span> <span class="p">},</span>
</span><span class='line'>  <span class="p">{</span> <span class="s2">&quot;sClass&quot;</span><span class="o">:</span> <span class="s2">&quot;role&quot;</span> <span class="p">},</span>
</span><span class='line'>  <span class="p">{</span> <span class="s2">&quot;sClass&quot;</span><span class="o">:</span> <span class="s2">&quot;city&quot;</span> <span class="p">}</span>
</span><span class='line'><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>The nRow parameter of the <code>fnRowCallback</code> function refers to the HTML row currently being drawn. In this case, if the column text happens to be
&#8216;admin&#8217;, replace the cells content with the jQuery generated img element:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="s2">&quot;fnRowCallback&quot;</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">nRow</span><span class="p">,</span> <span class="nx">aData</span><span class="p">,</span> <span class="nx">iDisplayIndex</span><span class="p">,</span> <span class="nx">iDisplayIndexFull</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.role&#39;</span><span class="p">,</span> <span class="nx">nRow</span><span class="p">).</span><span class="nx">text</span><span class="p">();</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="nx">role</span> <span class="o">==</span> <span class="s1">&#39;admin&#39;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">adminLabel</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;&lt;img/&gt;&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="s2">&quot;class&quot;</span><span class="o">:</span> <span class="s2">&quot;admin-icon&quot;</span> <span class="p">})</span>
</span><span class='line'>    <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;.role&#39;</span><span class="p">,</span> <span class="nx">nRow</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">adminLabel</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">nRow</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you wanted to be able to filter on this column too, one solution is to use the &#8216;text-indent&#8217; property to visually hide
text, leaving the image all that&#8217;s visible while still giving DataTables something to work with.</p>

<p>So the purpose of this post was to bring DataTables to your attention and to demonstrate how I&#8217;ve approached implementing the
functionality I desired. For many of your uses, the plugin will be plug and play – and you no longer have an excuse not to provide
these useful table interaction controls for your users.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Solution for osx lions wi-fi problems]]></title>
    <link href="http://mrjamesriley.github.com/blog/2011/08/20/solution-for-osx-lions-wi-fi-problems/"/>
    <updated>2011-08-20T17:23:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2011/08/20/solution-for-osx-lions-wi-fi-problems</id>
    <content type="html"><![CDATA[<p>My story is like many others out there: Having purchased a new Mac in the last few weeks, arriving pre-installed with Lion, I had a few days of bliss before the problems began. Failing to connect to my
wireless router, incredibly slow speeds when it did and unresponsive the majority of the time. Steve Jobs had ripped my heart out using nothing but a spoon.</p>

<p>I tried clearing various ram caches, recreating the wireless connection in network preferences, setting custom DNS, manual IP address, rebooting the router, performing dance rituals to the wireless dities etc and I was beginning to lose hope. Mention of an upcoming 10.7.1 update had me hopeful, but upon installation I had a few minutes of joy before the problems returned with all it&#8217;s troublesome tendencies. So what did work for me? <a href="http://bt.custhelp.com/app/answers/detail/a_id/9605/~/how-do-i-change-the-wireless-channel-on-my-bt-home-hub">Changing my wireless routers channel</a>, specifically to channel 4 (avoid anything over 11 for the BT HomeHub router).</p>

<!--more-->


<p>Now many have reported various fixes having worked but I&#8217;ve felt compelled to post this as not only have I got wireless working flawlessly on the Lion installed Mac Mini, but I&#8217;ve noticed a huge stability and performance increase. So regardless of whether you continue to have issues or not, switching up the wireless channel may work wizardry for you. Everything is sharper, more responsive and as a <a href="http://www.speedtest.net">broadband speed test</a> has shown me, more consistent, with the speed chart not varying over the course of the test. If you have BT Broadband, and in particular, you&#8217;re using an older HomeHub, give this a go and you may be surprised. The automatic channel selection works incorrectly in versions of the HomeHub prior to the latest version 3 from what I&#8217;ve read. Now, go forth and get back on that information super highway&#8230;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Nginx Error: no such file to load -- rack/commonlogger]]></title>
    <link href="http://mrjamesriley.github.com/blog/2011/03/20/nginx-error-no-such-file-to-load-rack-slash-commonlogger/"/>
    <updated>2011-03-20T06:07:00+00:00</updated>
    <id>http://mrjamesriley.github.com/blog/2011/03/20/nginx-error-no-such-file-to-load-rack-slash-commonlogger</id>
    <content type="html"><![CDATA[<p>Despite a lack of experience with server administration, I jumped in the deep end this week by grabbing a VPS from slicehost. With it, you start from scratch, with not even the firewall iptables setup to allow port 80. You&#8217;re given root access to ssh and sent on your way, which is one level above being given a screw driver and then directed to a pile of hardware components. But with frustration and confusion comes learning and knowledge&hellip; got me feeling pretty powerful over here.</p>

<p>The aim was to set up a Sinatra app, running with Nginx and Passenger. The installation of all was easy enough, up till the final step of pointing Nginx to my Sinatra app and seeing the following error: <code>Exception LoadError in application (no such file to load -- rack/commonlogger)</code></p>

<!--more-->


<p>Searches online suggest the issue could be down to file permissions, a poorly configured config.ru, or incorrect file paths. My config.ru correctly required rubygems and Sinatra (which depends on rack and thus includes it) and I tried everything from switching ruby versions to creating new users/groups.  Frustratingly, the issue was none of these, and the error never helped lead me to the solution. Passenger is smart enough to know if your app is a rack app by the existence of config.ru in the folder above the &lsquo;public&rsquo; folder specified in the Nginx config settings. It&#8217;s almost smart enough to call Bundler.setup() if you happen to have a Gemfile.</p>

<p>I had a Gemfile in my app, yet had removed Bundler and so wasn&#8217;t including the gem anywhere. The solution? remove the Gemfile.</p>

<p>This post does more to show my lack of passenger skills than it does for my community giving efforts but hopefully this&#8217;ll help someone even if that person is me, stumbling across this post in 6 months time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mac hanging at grey screen after deleting lib files]]></title>
    <link href="http://mrjamesriley.github.com/blog/2011/01/23/mac-hanging-at-grey-screen-after-deleting-lib-files/"/>
    <updated>2011-01-23T06:29:00+00:00</updated>
    <id>http://mrjamesriley.github.com/blog/2011/01/23/mac-hanging-at-grey-screen-after-deleting-lib-files</id>
    <content type="html"><![CDATA[<p>Is your Mac failing to boot past the grey screen, thanks to deleting some
important system files? The example below is specific, but the general method
to resolve should be the same in many cases: boot into single user mode and
re-install the missing program.</p>

<p>Having switched to a Mac after years of being solely a Windows user (and you
think you had a tough time growing up?!), every day is very much a learning
curve. Today was no different, while trying to get libxml2 working nicely with
the Nokogiri rubygem, I installed a newer version of the toolkit to a different
location. My Mac stubbornly insisted on using the current 2.6.16 version so I
bravely strolled into terminal and ran: <code>sudo rm /usr/lib/libxml*</code>.</p>

<!--more-->


<p>It turns out libxml2 is pretty fundamental to Mac and without it I was stuck.
Helpfully, I had no access to the Leopard installer and the CD drive on this
MacBook happens to be broken. Still, equiped with a bit of time, a Windows PC
nearby and a USB stick, here&rsquo;s the steps I took:</p>

<ol>
<li>Found out the exact software version I needed. As I am using version 10.5.8 of OSX10, <a href="http://www.explain.com.au/oss/libxml2xslt.html">this webpage</a> told me it&#8217;s libxml2 version 2.6.16 that&#8217;s built in, so this was what I went on the hunt for.</li>
<li>Grabbed the source code  from the <a href="http://xmlsoft.org/sources/old/">directory of old libxml2 versions</a> versions, before unpacking and copying the libxml2-2.6.16 to the USB stick.</li>
<li>Booted the Mac in Single user mode, by holding Command+S from the second you hit the power button. This sends you to a terminal prompt from where the fun begins.</li>
<li>Mounting the USB drive had to be done before making use of the contents.  This was done by simply following the steps detailed in <a href="http://macsage.com/mounting-usb-drive-in-single-user-mode" title="">this blog</a> article. This left me with my USB contents available within/Volumes/usb.</li>
</ol>


<p>From here, it was a simple case of installing the software with use of the standard linux program installation steps:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>./configure  
</span><span class='line'>make  
</span><span class='line'>make install</span></code></pre></td></tr></table></div></figure>


<p>Once complete, a reboot finally took me past the grey loading screen and back to where I was. Once the overwhelming emotion of being reunited with a loved one passed, I was back to developing. Lesson learnt: don&#8217;t be an idiot.  Never sticks with me.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Capistrano ignoring ssh public key]]></title>
    <link href="http://mrjamesriley.github.com/blog/2010/11/30/capistrano-ignoring-ssh-public-key/"/>
    <updated>2010-11-30T05:42:00+00:00</updated>
    <id>http://mrjamesriley.github.com/blog/2010/11/30/capistrano-ignoring-ssh-public-key</id>
    <content type="html"><![CDATA[<p>An issue that had me stumbled a while back and one of which I was reminded of
this morning: the initially frustrating issue of capistrano repeatedly asking
for my server ssh password. Now connecting to the deployment server via keyless
ssh works without fail, the deploy config file seems fine and you can connect
to your source code server via keyless ssh &ndash; so what gives?</p>

<p>A key aspect to remember is that the purpose of capistrano is to run commands
on your remote deployment server as if you were running them locally on the
server itself. So when we reach the task of grabbing the code from your chosen
code repository (cloning from git for example), the command is being called
from the deployment server and so it&rsquo;s this connection that requires the ssh
connection set up is order to avoid passwords.</p>

<p>The solution here is to set up the ssh key between your deployment server and
your source code repository server to authorise the connection, and this is
done in the usual way: running ssh-keygen on the deployment server, then
copying the generated .ssh/id_rsa.pub into a new line of your source code
servers .ssh/authorized_keys2 file. The details here may vary depending on your
setup.</p>

<p>Done, no more pestering &ndash; capistrano is supposed to improve productivity after
all right?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Thinking Sphinx on Rails setup issues]]></title>
    <link href="http://mrjamesriley.github.com/blog/2010/11/15/thinking-sphinx-on-rails-setup-issues/"/>
    <updated>2010-11-15T06:02:00+00:00</updated>
    <id>http://mrjamesriley.github.com/blog/2010/11/15/thinking-sphinx-on-rails-setup-issues</id>
    <content type="html"><![CDATA[<p>Sphinx is a full-text search server, able to index data from sources such as a database and make up for what traditional databases like MySQL lack. With use of the Thinking Sphinx gem, setup and usage with Ruby on Rails is real easy – yet there is a hurdle or two I stumbled upon:</p>

<!--more-->


<p>1) Searchd daemon cannot be found</p>

<p>This error occurred when attempting to deploy on production, and research on the internet yielded little in the way of solutions. The executable was present and included in the path, with the issue being down to running the thinking_sphinx:index rake task without specifying the rails environment.</p>

<p>Now this is not documented anywhere so there could be something I’ve missed during the configuration. The call:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'>  <span class="n">rake</span> <span class="n">thinking_sphinx</span> <span class="ss">:index</span> <span class="no">RAILS_ENV</span><span class="o">=</span><span class="n">production</span>
</span></code></pre></td></tr></table></div></figure>


<p>will do as expected, creating the configuration file based on the ‘define_index’ block in your Rails models and indexing as defined. A similar problem has caught many out before, such as leaving out the RAILS_ENV when performing a db:migrate. A more helpful error message from Thinking Sphinx would certainly be helpful.</p>

<p>2) Partial word searching</p>

<p>The Thinking Sphinx documentation mentions ‘Automatic Wildcards’ within its searching page yet this fails to work for partial words. Say I have an article titled ‘never enough’, the document would be retrieved if the search was for ‘never’, but not for simply ‘nev’. In my case, I’m using Thinking Sphinx during an auto complete search and so would require such functionality.</p>

<p>The solution is present within the ‘Advanced Sphinx Configuration’ page and all that’s required is to include the following line within your sphinx.yml in the config directory of your Rails application:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">min_prefix_len</span><span class="p">:</span> <span class="mi">3</span>
</span></code></pre></td></tr></table></div></figure>


<p>where 3 is the minimum number of letters till a search takes place. Note that this line can also be used within individual ‘define_index’ blocks if you do not want it to apply globally to all indexes within the same environment.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[jQuery ajax with Rails respond_to format.js]]></title>
    <link href="http://mrjamesriley.github.com/blog/2010/10/24/jquery-ajax-with-rails-respond-to-format-dot-js/"/>
    <updated>2010-10-24T08:50:00+01:00</updated>
    <id>http://mrjamesriley.github.com/blog/2010/10/24/jquery-ajax-with-rails-respond-to-format-dot-js</id>
    <content type="html"><![CDATA[<p>This year has been quite a shift for me in every aspect of my life &ndash;  especially with regard to web development and this blog will now reflect  that. For most part, my full time job and side projects use Ruby on  Rails and related technologies such as jQuery, mysql, memcache, redis and other web development aspects such as xhtml, css, usability and design. Future posts will announce projects of my own, document my discoveries and occasionally broadcast my opinions on all that is Web Development. Follow me. Let&rsquo;s jump straight in:</p>

<!--more-->


<p>There&#8217;s many resources out there that detail how to use jQuery with Ruby on Rails, specially how to handle the returning of data and the setting of javascript acceptance headers. Ryan Bates for example has a <a href="http://railscasts.com/episodes/136-jquery">Railscast</a> on the topic - yet in my case, I was not wanting to serialize and submit a form. If you simply want to post data to the server and insert the returned generated html into your page &ndash; simply use the jQuery.ajax with <code>dataType</code> set to <code>script</code>. From here, you can simply render the partial within the controllers <code>format.js</code> block and all should work without the need for any prior jQuery setup.</p>

<p>To demonstrate, here&#8217;s two code snippets copied straight from an application of mine:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'>  <span class="err">#</span> <span class="nx">the</span> <span class="nx">jquery</span>
</span><span class='line'>  <span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span>
</span><span class='line'>    <span class="nx">url</span><span class="o">:</span> <span class="s1">&#39;/polls/&#39;</span> <span class="o">+</span> <span class="nx">poll_id</span> <span class="o">+</span> <span class="s1">&#39;/vote?option_id=&#39;</span> <span class="o">+</span> <span class="nx">option_id</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">dataType</span><span class="o">:</span> <span class="s1">&#39;script&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;POST&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#poll&#39;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">data</span><span class="p">);</span> <span class="p">}</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'>
</span><span class='line'>  <span class="err">#</span> <span class="nx">the</span> <span class="nx">controller</span> <span class="nx">action</span>
</span><span class='line'>  <span class="nx">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nx">format</span><span class="o">|</span>
</span><span class='line'>    <span class="nx">format</span><span class="p">.</span><span class="nx">html</span> <span class="p">{</span> <span class="nx">redirect_to</span> <span class="s1">&#39;/&#39;</span> <span class="p">}</span>
</span><span class='line'>    <span class="nx">format</span><span class="p">.</span><span class="nx">js</span> <span class="p">{</span>
</span><span class='line'>      <span class="nx">render</span> <span class="o">:</span><span class="nx">partial</span> <span class="o">=&gt;</span> <span class="s1">&#39;widget&#39;</span><span class="p">,</span> <span class="o">:</span><span class="nx">locals</span> <span class="o">=&gt;</span> <span class="p">{</span> <span class="o">:</span><span class="nx">poll</span> <span class="o">=&gt;</span> <span class="err">@</span><span class="nx">poll</span> <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="nx">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Till next time!</p>
]]></content>
  </entry>
  
</feed>

