<?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; rails3</title>
	<atom:link href="http://jameswilding.net/tag/rails3/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>How To Use RSpec with Rails 3</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2010%2F09%2F04%2Fhow-to-use-rspec-with-rails-3%2F&amp;seed_title=How+To+Use+RSpec+with+Rails+3</link>
		<comments>http://jameswilding.net/2010/09/04/how-to-use-rspec-with-rails-3/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 18:38:25 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=731</guid>
		<description><![CDATA[Because Rails 3 is still pretty new (less than a week old as I write this), there&#8217;s relatively little documentation about how to use RSpec with this latest version of Rails [update: what I should have said is that docs for Rspec and Rails 3 are good, but perhaps less well-organised than for older versions of [...]]]></description>
			<content:encoded><![CDATA[<p>Because Rails 3 is still pretty new (less than a week old as I write this), <del datetime="2010-09-05T09:37:57+00:00">there&#8217;s relatively little documentation about how to use RSpec with this latest version of Rails</del> [update: what I should have said is that docs for Rspec and Rails 3 are good, but perhaps less well-organised than for older versions of Rails. See comments below.].What I want to do here is bring together the instructions I&#8217;ve found on using RSpec with Rails 3, and share some tips on how to get your BDD cycle running smoothly.</p>
<p>[Note: from this point on, I'll use "Rails" to mean "Rails 3"]</p>
<h3>Installing Rspec for Rails 3</h3>
<p>The first new thing to be aware of is the rspec-rails gem; because Rails 3 is much more modular and extensible than previous versions of the framework, this one gem can do all the work of plugging RSpec into your Rails apps.</p>
<p>Install the gem thus:</p>
<pre><code>gem install rspec-rails --pre</code></pre>
<p>The <code>--pre</code> tells Rubygems to install the beta version of the gem, which you&#8217;ll need for Rails 3 development.</p>
<p>The next step is to tell your Rails app to use the rspec-rails gem; this is done quite simply in your app&#8217;s Bundle file. Because you don&#8217;t need RSpec in production, add the gem to the development and test groups only:</p>
<pre><code># in path/to/your/app/Bundle
group :development, :test do
  # the version number may be different for you.
  # Use gem list rspec-rails --local on your command line
  # to get the exact version number.
  gem 'rspec-rails', '2.0.0.beta.20'
end
</code></pre>
<p>The final step is to run the following in the root of your Rails app:</p>
<pre><code>script/rails generate rspec:install</code></pre>
<p>This sets up a spec/ folder and some helper files.</p>
<h3>Using RSpec</h3>
<p>Now you&#8217;ve installed the rspec-rails gem, using RSpec is actually really simple. The gem tells Rails to use rspec to generate test files, which means that generators will create spec files without you having to pass in any extra options (if you check the options for <code>rails generate model</code>, you&#8217;ll see that there&#8217;s a <code>--test-framework</code> option. You <em>don&#8217;t</em> need to use this to specify RSpec; after installing rspec-rails, RSpec will be used by default).</p>
<p>So, to generate a Person model with corresponding person_spec.rb, all you need to do is this (the same applies to controllers, helpers, etc):</p>
<pre><code>rails generate model Person</code></pre>
<p>Check out spec/models/person_spec.rb to see the stub spec for your Person model. That&#8217;s really all you need to know to start using RSpec on Rails 3, but do checkout <a href="http://github.com/rspec/rspec-rails">the rspec-rails repository on Github</a>: the README gives more in-depth explanations of a lot of the points I&#8217;ve made here, and also has extra tips on writing specs for controllers, views, responses, and routes.</p>
<h3>Pro Tips</h3>
<p>A few cools things I&#8217;ve found using RSpec in my new Rails apps:</p>
<h4>Helper Specs</h4>
<p>Helpers specs have access to a special object in the <code>helper</code> object, which includes your helper. For example, in person_helper_spec.rb the helper object has access to all the methods in PersonHelper. This makes it really easy to test your helper methods:</p>
<pre><code># Assumes a PersonHelper with a #speak method.
describe PersonHelper do
  describe '#speak' do
    # roughly equivalent to:
    #
    # helper = Object.new
    # helper.send :include, PersonHelper
    # helper.speak.should == 'Hello'
    it 'says Hello' do
      helper.speak.should == 'Hello'
    end
  end
end
</code></pre>
<h4>Spec Support Files</h4>
<p>Although it&#8217;s not there by default, if you create a folder spec/support, then every file under that folder will be required when you run specs. This is really useful if you want to keep custom RSpec matchers, mocks, or other supporting code, in separate files (for example, you might want to keep custom matcher code in spec/support/my_matcher.rb and mocks in spec/support/mocks.rb).</p>
<p>Hopefully this post has helped you can a handle on how to use RSpec with the latest version of Rails. If you spot any other &#8220;pro tips&#8221;, post them in the comments! Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2010/09/04/how-to-use-rspec-with-rails-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
	</channel>
</rss>
