<?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-01-04T06:26:58+00: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[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 discovers 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>

