<?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>James Wilding&#039;s Weblog &#187; rails</title>
	<atom:link href="http://jameswilding.net/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameswilding.net</link>
	<description>Buddhist businessman, freelance web developer</description>
	<lastBuildDate>Sun, 05 Sep 2010 09:38:56 +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>Paperclip on Rails 3</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F07%2F24%2Fpaperclip-rails-3%2F&amp;seed_title=Paperclip+on+Rails+3</link>
		<comments>http://jameswilding.net/2010/07/24/paperclip-rails-3/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 10:56:39 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=695</guid>
		<description><![CDATA[You want to run Rails 3. You want to use the excellent Paperclip plugin to mess with your attached files. Not a problem. It Just Works Paperclip on Rails 3 now just works. Back in February 2010, I wrote about how to use Paperclip on an early beta release of Rails 3 &#8212; back then, [...]]]></description>
			<content:encoded><![CDATA[<p>You want to run Rails 3. You want to use <a href="http://github.com/thoughtbot/paperclip">the excellent Paperclip plugin</a> to mess with your attached files. Not a problem.</p>
<h3>It Just Works</h3>
<p>Paperclip on Rails 3 now just works.</p>
<p><a href="http://jameswilding.net/2010/02/07/paperclip-on-rails-3-beta/">Back in February 2010</a>, I wrote about how to use Paperclip on an early beta release of Rails 3 &#8212; back then, a few small hacks were required to get Paperclip running. Now Rails 3 is at beta 4, and it&#8217;s safe to assume that we&#8217;re as close as we can be to an official Rails release &#8212; minus a few last minute tweaks, of course &#8212; and both the framework itself and the Paperclip plugin are more stable and more compatible. The good news, then, is that it&#8217;s very easy to use the Rails 3 and Paperclip together.</p>
<h3>How To Use Paperclip With Rails 3</h3>
<p>These instructions are good for Rails 3 beta 4, and Paperclip <a href="http://github.com/thoughtbot/paperclip/commit/9223a917a821731986603e89a9b2e4d1a4fdd68a">9223a917</a>. They&#8217;ll probably work on other versions of Rails and Paperclip, too.</p>
<ol>
<li>Install Rails 3</li>
<li>Create a new Rails app</li>
<li>Install Paperclip</li>
<li>Create a model</li>
<li>Create a controller and views</li>
<li>Define your routes</li>
</ol>
<p>You can find the important files (controller, model, views, and routes) in <a href="http://gist.github.com/488579">this gist</a>, and the full application code is <a href="http://github.com/jameswilding/paperclip_example">available on Github</a>.</p>
<h4>Install Rails 3</h4>
<p>Install the latest prerelease of Rails with the following:</p>
<pre>$ gem install rails --pre</pre>
<h4>Create a new Rails app</h4>
<p>The command for creating Rails apps has changed in Rails 3. In the bright new future, everything is done using &#8220;rails&#8221; <a class="simple-footnote" title="I&#8217;ve found that, on Mac OSX 10.6, I have to use &#8220;/usr/bin/rails&#8221; (that&#8217;s where my Rails binary lives) instead of &#8220;rails&#8221;. No doubt that will be fixed soon." id="return-note-695-1" href="#note-695-1"><sup>1</sup></a>:</p>
<pre>$ rails new paperclip_example</pre>
<h4>Install Paperclip</h4>
<p>Paperclip&#8217;s master branch is now good to go with Rails 3. Install from Github thus:</p>
<pre>$ rails plugin install git://github.com/thoughtbot/paperclip.git</pre>
<h4>Create a model</h4>
<p>In order to work with Paperclip, your model needs a few special database columns. In this case, I&#8217;m creating a User class which will have an attached avatar image.</p>
<pre>$ rails generate model User \
avatar_file_name:string \
avatar_content_type:string \
avatar_file_size:integer \
avatar_updated_at:datetime
$ rake db:migrate</pre>
<p>I use Paperclip&#8217;s has_attached_file method to define my avatar attachment:</p>
<pre>class User &lt; ActiveRecord::Base
  has_attached_file :avatar, :styles =&gt; {
    :thumb =&gt; '50x'
  }
end</pre>
<h4>Create a controller and views</h4>
<p>I&#8217;m setting up a simple UsersController with index, new, and create actions (and corresponding routes). You&#8217;ll probably want to go further, but if all you&#8217;re interested in is proof-of-concept then this is enough.</p>
<pre>$ rails generate controller Users</pre>
<p>See <a href="http://gist.github.com/488579">this gist</a> for the code in my UsersController, and the HTML in my views.</p>
<h4>Define your routes</h4>
<p>In config/routes.rb:</p>
<pre>PaperclipExample::Application.routes.draw do |map|
  resources :users, :only =&gt; [:index, :new, :create]
  root :to =&gt; 'users#index'
end</pre>
<p>Lastly, if you&#8217;re mapping your app&#8217;s root to a controller, remember to delete public/index.html.</p>
<h3>And You&#8217;re Done</h3>
<p>That&#8217;s really all there is to it. Use rails server to start your app and view the results. Did it work for you?</p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-695-1">I&#8217;ve found that, on Mac OSX 10.6, I have to use &#8220;/usr/bin/rails&#8221; (that&#8217;s where my Rails binary lives) instead of &#8220;rails&#8221;. No doubt that will be fixed soon. <a href="#return-note-695-1">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/07/24/paperclip-rails-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Paperclip on Rails 3 Beta</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F02%2F07%2Fpaperclip-on-rails-3-beta%2F&amp;seed_title=Paperclip+on+Rails+3+Beta</link>
		<comments>http://jameswilding.net/2010/02/07/paperclip-on-rails-3-beta/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 19:30:43 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=504</guid>
		<description><![CDATA[More fun with Rails 3 beta.]]></description>
			<content:encoded><![CDATA[<p class="update"><strong>Update:</strong> you should <a href="http://jameswilding.net/2010/07/24/paperclip-rails-3/">read this more recent article</a> for an explanation of how to use Paperclip with the latest beta release of Rails 3.</p>
<p>I got Paperclip working on Rails 3 today. Here&#8217;s how.</p>
<p>First, I found (by accident) <a href="http://github.com/thoughtbot/paperclip/tree/rails3">a branch in the Paperclip repository</a> on Github called &#8220;Rails 3&#8243;. Call me crazy but I thought that might be worth a try.</p>
<p>Normally you&#8217;d install Rails 3 plugins using rails plugin install, but in this case I used git submodule:</p>
<p><script src="http://gist.github.com/298015.js?file=gistfile1.txt"></script></p>
<p>These two commands checkout the rails3 branch of Paperclip into vendor/plugins/paperclip, as a Git submodule. If you don&#8217;t understand Git submodules, you can <a href="http://book.git-scm.com/5_submodules.html">read about them here</a>.</p>
<p>I also found that Paperclip wasn&#8217;t properly handling image styles; the &#8216;Paperclip&#8217; section of <a href="http://jameswilding.net/2010/02/07/working-with-rails-3/">this post</a> explains how I fixed this (a word of warning: it&#8217;s a simple one-line edit which solved my problem but is untested with regard to the rest of Paperclip).</p>
<p>As always, feel free to post questions or feedback in the comments. Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/02/07/paperclip-on-rails-3-beta/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Working with Rails 3</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F02%2F07%2Fworking-with-rails-3%2F&amp;seed_title=Working+with+Rails+3</link>
		<comments>http://jameswilding.net/2010/02/07/working-with-rails-3/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 17:27:22 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails3]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=499</guid>
		<description><![CDATA[Don't be scared, you can actually make a working app with Rails 3 beta.]]></description>
			<content:encoded><![CDATA[<p>So it&#8217;s Sunday, and rather than go to church I decided to take Rails 3 for a test drive. As you do. How did it go? In the main, very smoothly &#8212; a couple of bugs, which you Googlers might find it useful to know about, and some nice new stuff.</p>
<h3>Bug: the rails command</h3>
<p>Failed completely :) Rails my_app fails with &#8220;no value provided for required arguments &#8216;app_path&#8217;&#8221;. The solution to this is easy, though: first, get a local copy of Rails 3 (I cloned the Rails repo: git clone git://github.com/rails/rails.git) and then use this command instead:</p>
<p><script src="http://gist.github.com/297529.js?file=gistfile1.sh"></script> The rubygems require is necessary for Rails 3 to load all the gems it needs to run.</p>
<h3>Thoughts</h3>
<h4>The Rails command</h4>
<p>In Rails 3, the rails command replaces script/console, script/server, etc. This is nice: commands are more meaningful (run the rails server with &#8220;rails server&#8221;), and easier to type.</p>
<h4>ActiveRecord&#8217;s new finder syntax</h4>
<p>ActiveRecord&#8217;s new finder syntax is lovely, and has some nice performance gains which you can read about on other blogs. Named scopes look good:<br />
<script src="http://gist.github.com/297532.js?file=gistfile1.rb"></script></p>
<p>I&#8217;m going to write more about this soon.</p>
<h4>Speed</h4>
<p>Call me crazy but Rails 3 seems faster. Maybe this has something to do with the fact that it uses <a title="Erubis" href="http://www.kuwata-lab.com/erubis/">Erubis</a> instead of ERB, or maybe I&#8217;m just easily impressed :)</p>
<h4>Paperclip (tiny bug)</h4>
<p>Paperclip has <a href="http://github.com/thoughtbot/paperclip/tree/rails3">a branch called Rails3</a>: I went out on a limb and guessed that was for Rails 3. It almost just worked, except for one thing: custom attachment styles weren&#8217;t being created.</p>
<p>If you&#8217;ve used paperclip, you&#8217;ll know you can do this:</p>
<p><script src="http://gist.github.com/297539.js?file=gistfile1.rb"></script> Well, those :medium, :wide, and :small variations weren&#8217;t being created. I didn&#8217;t have time to investigate properly but this seemed to have something to do with Paperclip&#8217;s callbacks (which were returning false even when they shouldn&#8217;t be): my one line hack/fix for this:  </p>
<p><script src="http://gist.github.com/297537.js?file=gistfile1.rb"></script></p>
<p>I freely admit that this might break other parts of Paperclip, but it works for me.</p>
<h3>Conclusions</h3>
<p>Overall, Rails 3 feels very nice. As you&#8217;d expect from a beta it almost just works, and I&#8217;ve made a working app with it in a day. Super easy! Next up: deploying to Heroku.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/02/07/working-with-rails-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Lessons Learnt With Rails</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F01%2F21%2Flessons-learnt-with-rails%2F&amp;seed_title=Lessons+Learnt+With+Rails</link>
		<comments>http://jameswilding.net/2010/01/21/lessons-learnt-with-rails/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 13:14:02 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=423</guid>
		<description><![CDATA[Things I've learnt from working on a massive Rails application that needed tidying. Routes, simplicity, Ruby, REST -- all sorts of Rails goodness.]]></description>
			<content:encoded><![CDATA[<p>Some lessons I&#8217;ve learnt working on my latest Rails project, a piece of tidy-and-fix work on an existing Rails application. Working on this project has really taught me a lot about the right and wrong ways to use Rails.</p>
<h3>1. Routes are important</h3>
<p>The app that I was asked to rebuilt had no routes. Nothing.</p>
<p>That&#8217;s not to say that no URLs worked with the app, just that the mappings between URLs and controllers/actions was defined in the code itself &#8212; mostly using the old Rails convention:</p>
<p><script src="http://gist.github.com/282785.js?file=urlfor.rb"></script></p>
<p>This makes it very difficult to discover how a Rails app responds to a particular URL, without hunting through the controllers to find a corresponding action. It also makes working with REST a nightmare!</p>
<p><strong>Lesson learnt:</strong> use routes.rb</p>
<h3>2. Conventions are important</h3>
<p>Rails is all about convention over configuration, of course. But when you&#8217;re coding a Rails app &#8212; especially if you&#8217;re new to Rails &#8212; it&#8217;s easy to ignore those conventions, or just be plain unaware of them.</p>
<p>This leads to a lot of unnecessary work. I&#8217;ve spent a lot of time over the past few months translating unconventional code into code that more closely fits the Rails way of doing things: the result is code that&#8217;s easier to test, easier to maintain, and much, much faster.</p>
<p><strong>Lesson learnt: </strong>take some time to understand Rails&#8217; conventions</p>
<h3>3. Simplicity is important</h3>
<p>The more I write code, the more I find myself thinking: complicated code is a Bad Thing. Of course, as a kind pedant once told me, there&#8217;s a difference between <em>complicated</em> and <em>complex</em>.</p>
<p>(If you don&#8217;t have a dictionary handy, &#8220;complicated&#8221; roughly means messy, difficult to understand; &#8220;complex&#8221; means many interconnected parts, or a large system. The key difference is that complex systems can be well designed: <em>complicated</em> systems are not.)</p>
<p>OK, dictionary rant over :) Messy, complicated Rails applications come about when a developer doesn&#8217;t take the time to think things through. Simplicity is normally easy if you take some time to plan your work upfront &#8212; messy code happens when things are rushed or not thought through.</p>
<p><strong>Lesson learnt:</strong> think before you start coding</p>
<h3>4. Ruby is important</h3>
<p>Obvious, but very true: Ruby is important to Rails applications. If you&#8217;re new to coding, or new to Rails, then take some time to learn Ruby first: otherwise, you&#8217;ll miss out on the power (and beauty) of a great language and a great framework.</p>
<p><strong>Lesson learnt: </strong>learn Ruby <em>and</em> Rails, not just Ruby <em>on </em>Rails!</p>
<h3>You are important, too</h3>
<p>Conclusions? All of the above (and more) is important when you&#8217;re developing a Rails app. And you, the developer, are important too &#8212; have fun, keep learning, and don&#8217;t work too hard. Rails should be fun :)</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/01/21/lessons-learnt-with-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clone TinyURL (and friends) with Rails</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2009%2F08%2F11%2Ftinyurl-rails%2F&amp;seed_title=Clone+TinyURL+%28and+friends%29+with+Rails</link>
		<comments>http://jameswilding.net/2009/08/11/tinyurl-rails/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 13:28:58 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=170</guid>
		<description><![CDATA[tr.im is closing down, and the blogs are a-chatter about why URL-shortening services are probably a bad idea if you care about your links, and why you should roll your own service if you can. Which is where Rails comes in. It&#8217;s possible to clone TinyURL using Sinatra, but I&#8217;m more interested in Rails (I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tr.im/">tr.im is closing down</a>, and the blogs are a-chatter about why URL-shortening services are probably a <a href="http://www.zeldman.com/2009/08/10/shorten-this/">bad idea</a> if you care about your links, and why you should <a href="http://daringfireball.net/linked/2009/08/10/trim">roll your own service</a> if you can.</p>
<p>Which is where Rails comes in.</p>
<p><span id="more-170"></span><br />
It&#8217;s possible to clone TinyURL <a href="http://blog.saush.com/2009/04/13/clone-tinyurl-in-40-lines-of-ruby-code/">using Sinatra</a>, but I&#8217;m more interested in Rails (I think Rails probably scales better). The basic setup is so simple, it doesn&#8217;t take long to write the code, and with Rails you can always add extras like an admin interface later without much fuss.</p>
<h3>The code</h3>
<p>(If you&#8217;re feeling lazy, the entire URL-shortening app is <a href="http://github.com/jameswilding/shrink/">available on Github</a>)</p>
<p>My TinyURL-like Rails app &#8212; I call it &#8220;Shrink&#8221; &#8212; has three main components: routes, controller, and model. Routes first:</p>
<p>[ruby]<br />
ActionController::Routing::Routes.draw do |map|</p>
<p>  map.resources :locations, :only =&gt; [:new, :create]</p>
<p>  map.with_options :controller =&gt; &#8216;locations&#8217; do |map|<br />
    map.root :action =&gt; &#8216;new&#8217;<br />
    map.preview   &#8216;/:shrunken/preview&#8217;, :action =&gt; &#8216;preview&#8217;<br />
    map.location  &#8216;/:shrunken&#8217;, :action =&gt; &#8216;redirect&#8217;<br />
  end</p>
<p>end<br />
[/ruby]</p>
<p>&#8220;Locations&#8221; are the web pages we&#8217;ll be redirecting people to when they click on a short URL.</p>
<p>As you can probably tell, this routes file gives us RESTful routes for creating new locations, along with a catch-all <code>shrunken</code> route which we&#8217;ll use to intercept our short URLs and redirect them. There&#8217;s also a <code>preview</code> route (more on this in a bit), and I&#8217;ve mapped the root path (&#8216;/&#8217;) to the <code>new</code> action on <code>LocationsController</code> &#8212; this will expose the form for shortening URLs on our site&#8217;s home page.</p>
<h3>The Controller</h3>
<p>We only need one controller, with just a few short methods:</p>
<p>[ruby]<br />
class LocationsController &lt; ApplicationController</p>
<p>  def new<br />
    @location = Location.new<br />
  end</p>
<p>  def preview<br />
    @location = Location.find_by_short_url(params[:shrunken])<br />
  end</p>
<p>  def redirect<br />
    @location = Location.find_by_short_url(params[:shrunken])<br />
    redirect_to @location.url<br />
  end</p>
<p>  def create<br />
    @location = Location.new(params[:location])</p>
<p>    if @location.save<br />
      redirect_to preview_path(@location)<br />
    else<br />
      flash[:notice] = &#8216;Invalid URL&#8217;<br />
      render :action =&gt; &quot;new&quot;<br />
    end<br />
  end</p>
<p>end<br />
[/ruby]</p>
<p>That&#8217;s simple enough &#8212; the only thing to note is that we&#8217;re using <code>Location. find_by_short_url</code> instead of <code>Location.find</code>, so we can use slugs like &#8216;a1b3c&#8217; instead of database record IDs in our URLs.</p>
<p>The preview action, by the way, just renders a view which shows the user the shortened version of their URL so they can copy and paste it into Twitter (or whatever).</p>
<h3>The Model</h3>
<p>[ruby]<br />
class Location &lt; ActiveRecord::Base</p>
<p>  validates_presence_of :url<br />
  before_create         :validate_url</p>
<p>  class &lt;&lt; self<br />
    def find_by_short_url(shrunk)<br />
      find(shrunk.to_i(36))<br />
    end<br />
  end</p>
<p>  def to_param<br />
    id.to_s(36)<br />
  end</p>
<p>  private<br />
  def validate_url<br />
    false unless url_valid?(URI.parse(self.url))<br />
  end</p>
<p>  def url_valid?(url)<br />
    url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)<br />
  end</p>
<p>end<br />
[/ruby]</p>
<p>The model implements our <code>find_by_short_url method</code>, implements some validations, and overrides Rails&#8217; default <code>to_param</code> to provide TinyURL-like short URLs. <code>to_param</code> is used in <code>url_for</code>; the method normally returns an object&#8217;s database ID, for URLs like /people/1, but here we return a short string like &#8216;a1c&#8217; instead.</p>
<p>Note the calls to to_s(36) and to_i(36): these convert record IDs into shorter URL slugs, and back again, using base 36 numbering. By using base 36, we can keep our URLs as short as possible: 10000 in base 10 is &#8217;7ps&#8217; in base 36. Read more about this useful feature of Ruby at <a href="http://www.ruby-doc.org/core/classes/String.html#M000787">ruby-doc.org</a>.</p>
<h3>Putting it all together</h3>
<p>The code for this app is open-source and is <a href="http://github.com/jameswilding/shrink">available on Github</a>; download, run script/server, and go to http://0.0.0.0:3000/ and you&#8217;ll see it in action. Proof that you can clone TinyURL using Rails in less than an hour!</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2009/08/11/tinyurl-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>iPhone on Rails</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2009%2F08%2F10%2Fiphone-on-rails%2F&amp;seed_title=iPhone+on+Rails</link>
		<comments>http://jameswilding.net/2009/08/10/iphone-on-rails/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 09:27:41 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=153</guid>
		<description><![CDATA[It&#8217;s really simple to get a Rails application to serve custom views to an iPhone or iPod touch: so simple, in fact, that in a spare half-an-hour I wrapped the necessary code up into a tiny plugin, which will do all the work for you! Update: if you&#8217;re using the plugin, have questions, or have [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s really simple to get a Rails application to serve custom views to an iPhone or iPod touch: so simple, in fact, that in a spare half-an-hour I wrapped the necessary code up into <a href="http://github.com/jameswilding/iphoneification">a tiny plugin</a>, which will do all the work for you!</p>
<p><strong>Update:</strong> if you&#8217;re using the plugin, have questions, or have found this post useful, take a moment to <a title="My Github profile" href="http://github.com/jameswilding">send me a message on Github</a>, or email me (james at this domain). It&#8217;s always great to know how people are using my work :)</p>
<p><span id="more-153"></span></p>
<p>If you just want to get up and running as fast as possible, skip to the <strong>Plugin</strong> section at the end of the article. Feedback is always appreciated; if you use the plugin, or have questions, feel free to email me (james at this domain).</p>
<p>If you want to understand how the plugin works, read the full article.</p>
<h3>Three Simple Steps</h3>
<p>There are basically three steps to serving custom iPhone content from Rails:</p>
<ol>
<li>Register a custom <code>iphone</code> format</li>
<li>Tell Rails how to recognize an iPhone request</li>
<li>Tell Rails to use the custom format for iPhone requests</li>
</ol>
<h3>Registering a custom iPhone format</h3>
<p>Rails already knows about formats like HTML and XML, and all iPhones actually need from a Rails app is plain old HTML &#8212; but we need a way to distinguish our iPhone HTML from the HTML we send to other browsers.</p>
<p>To do this, we register a mime type alias &#8212; just a way of telling Rails that when we say &#8216;iphone&#8217;, we mean &#8216;HTML&#8217;. This is done in config/initializers/mime_types.rb:</p>
<p>[ruby]<br />
Mime::Type.register_alias &#8220;text/html&#8221;, :iphone<br />
[/ruby]</p>
<p>Now we can do something like <code>format.iphone</code> and Rails will know we want it to serve our views as HTML.</p>
<h3>Tell Rails how to recognize an iPhone request</h3>
<p>In order to serve custom views to iPhones, we need a way of recgonizing when someone is viewing our website on an iPhone. This is easy:</p>
<p>[ruby]<br />
class ApplicationController &lt; ActionController::Base</p>
<p># other code here</p>
<p>private<br />
# detect iphone requests<br />
def iphone_request?<br />
(agent = request.env["HTTP_USER_AGENT"]) &amp;&amp;<br />
agent[/(Mobile\/.+Safari)/]<br />
end<br />
end<br />
[/ruby]</p>
<p>The <code> iphone_request?</code> method looks for MobileSafari&#8217;s <a href="http://en.wikipedia.org/wiki/User_agent">user agent</a> string in the incoming HTTP request. Because <a href="http://www.apple.com/iphone/iphone-3gs/safari.html">MobileSafari</a> runs on the iPod Touch as well as on the iPhone, this method will ensure that we serve our custom views to both devices.</p>
<p>Now we can identify iPhone requests, we just need to put everything together and&#8230;</p>
<h3>Tell Rails to use our custom format for iPhone requests</h3>
<p>Normally, when Rails receives a request from a browser, it determines what sort of response the browser is looking for (XML, HTTP, JSON, etc). Then it looks for corresponding templates &#8212; index.html.erb, index.xml.erb, etc. This behaviour is customizable using ActionController&#8217;s <code>respond_to</code> method.</p>
<p>In order to serve custom views to the iPhone, we need to tell Rails to switch to our custom :iphone format whenever an iPhone visits our website. This sounds complicated, but is easy:</p>
<p>[ruby]<br />
class ApplicationController &lt; ActionController::Base</p>
<p>before_filter :adjust_format_for_iphone_requests</p>
<p>private<br />
def adjust_format_for_iphone_requests<br />
request.format = :iphone if iphone_request?<br />
end<br />
end<br />
[/ruby]</p>
<p>This little snippet calls our <code>iphone_request?</code> method and tells Rails to use our iphone format where appropriate.</p>
<p>That&#8217;s really all there is to it. You can now create index.iphone.erb, show.iphone.erb, etc, alongside your standard HTML views and layouts, and Rails will serve them up to iPhone users. Bon appetit!</p>
<h3>The Plugin</h3>
<p>Serving custom iPhone content is something I want to do from many Rails apps, so I wrapped the functionality outlined above into <a href="http://github.com/jameswilding/iphoneification">a plugin for quick and easy iphoneification</a>. Install thus:</p>
<p>[bash]<br />
script/plugin install git://github.com/jameswilding/iphoneification.git<br />
[/bash]</p>
<p>To use the plugin, just add one line to your application controller:</p>
<p>[ruby]<br />
class ApplicationController<br />
responds_to_iphone<br />
end<br />
[/ruby]</p>
<p>If there&#8217;s a particular controller or action that you don&#8217;t want to serve iPhone content from, use</p>
<p>[ruby]<br />
class NotAnIPhoneController<br />
skip_iphone_response<br />
end<br />
[/ruby]</p>
<p>The plugin does most of the work: all you need to do is name your custom iPhone template <code>{template_name}.iphone.erb</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2009/08/10/iphone-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 3</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2009%2F08%2F05%2Frails-3%2F&amp;seed_title=Rails+3</link>
		<comments>http://jameswilding.net/2009/08/05/rails-3/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 15:34:26 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=140</guid>
		<description><![CDATA[Well, I&#8217;ve been away from WordPress for a while thanks to flu (swine? maybe), and I thought a good way to break the silence would be with a post on the upcoming release of Rails: version 3. As you may know, Rails 3 is a major re-working of the Rails internals &#8212; and &#8216;internals&#8217; is [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I&#8217;ve been away from WordPress for a while thanks to flu (swine? maybe), and I thought a good way to break the silence would be with a post on the upcoming release of Rails: version 3.</p>
<p><span id="more-140"></span></p>
<p>As you may know, Rails 3 is a major re-working of the Rails internals &#8212; and &#8216;internals&#8217; is the important word. Rails 3 will <em>not</em> introduce any major changes the the way Rails works for the normal Rails developer. If you compare Rails 3 application code to Rails 2 application code, you won&#8217;t see much difference.</p>
<p>Where you will see differences is in the Rails framework code &#8211; the stuff that implements features like routes, <code>respond_to</code>, <code>render</code>, and resources. Its fair to say that this code has undergone major changes &#8212; the guts have been ripped out and the whole framework has been put back together in a new way.</p>
<p>Of course it&#8217;s a testament to Rail&#8217;s strengths as a framework that a major refactoring of its code base is possible. It&#8217;s also testament to the skill of the people who are working on the code &#8212; the original Rails core team, plus new members from Merb. I&#8217;ve been following <a href="http://yehudakatz.com/">Yehuda Katz&#8217;s blog</a> (he&#8217;s one of the guys who developed <a href="http://merbivore.com/">Merb</a>) and the things he&#8217;s written about his work on Rails 3 have been really interesting.</p>
<p>In particular, Katz&#8217;s approach focuses more on the benefits of a well-organised Rails code base, and less on the pragmatic &#8220;make it cool as fast as possible&#8221; approach which seems to have characterised Rails up to now. That&#8217;s not to knock the later &#8212; Rails wouldn&#8217;t be where it is today if people hadn&#8217;t been willing to turn a blind eye to overly-complicated code or poor internal structures.</p>
<p>But these new changes are good: Rails will be as easy and as fun to use for serious developers (of plugins, for instance) as it always has been for front-end designers and application builders. It really feels like we&#8217;re getting the best of both worlds.</p>
<p>If you&#8217;re interested in learning more about the philosophy behind Rails 3, I recommend the following:</p>
<ul>
<li>Yehuda Katz&#8217;s <strong><a href="http://yehudakatz.com/tags/ruby/ruby-on-rails/">posts about his work refactoring Rails</a></strong></li>
<li><strong><a href="http://railsconf.blip.tv/file/2081411/">DHH&#8217;s RailsConf keynote</a></strong></li>
<li><strong><a href="http://skillsmatter.com/podcast/ajax-ria/yehuda-katz-interfaces-and-the-future-of-ruby-keynote">This presentation</a></strong> from a London Rails conference, which explains more about the thinking behind the Rails refactor. Treat the video like a radio show, because the picture quality is pretty bad.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2009/08/05/rails-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
