Elliott J Draper, Freelance Developer
Blog
Posts tagged "rails":autotest
I’ve just recently started using autotest again, and it’s making development so much easier, and more enjoyable. If you do any TDD (test driven development) with Ruby, you need autotest running, as it really improves the experience massively.
With autotest, and growl, you can plod away writing failing tests, implementing code so that they pass, and then rinse and repeat - all the way getting messages flash up telling you when tests are broken, and when tests are passing again. It’s a great way to iterate through writing code to meet user requirements, and incredibly satisfying as you turn failing tests into passing ones, with working code.
So what do you need to do to get it setup? You’ll need to install ZenTest, which contains autotest amongst some other tools:
sudo gem install ZenTest
You can then install the following gems too if you don’t have them already:
sudo gem install autotest-growl
sudo gem install autotest-rails
Finally, you’ll need to setup a small config file, called .autotest in your home directory (~/.autotest):
require "autotest/growl"
That’s all you need in that file, now just hop into a Rails project directory, and run autotest:
cd /path/to/project
autotest
You’ll then get output from the initial test run, and from now on while autotest is running, when you edit a file, it will re-run any tests it deems to have been impacted by a change to that file. It will pop up test failures and test successes as growl notifications too. To force it to re-run the entire test suite, press ctrl+c once. To end autotest completely, use ctrl+c twice in quick succession. Bear in mind for things like database schema changes, you’ll need to do the following to get the test database schema up to date and inline with the development database, otherwise your tests when being run under autotest will fail:
rake db:test:prepare
Now get cracking with those user stories, get those tests written, and then turn them from red to green!
Ruby on Rails Tutorial
Not a great start to my yearly goals - right after posting about how I want to blog every other day, I manage to go six days without a post! Time to try again, starting from today…
I was planning my next article as a follow-up to the Getting started with Rails 2.3.5 article I did in December, when I stumbled across this rather brilliant work in progress online tutorial book, Ruby on Rails Tutorial, by Michael Hartl (author of the great Railsspace book). Rather than write some more beginner articles that are probably covered by that material (or soon will be), I figured I’d point my readers in the direction of that resource, as it is certainly shaping up to be a great read for anyone just getting started with Rails.
So going forward I’m going to concentrate on some more advanced topics, and a few more interesting things perhaps for those who are already up to speed with the Rails basics. I’m going to be covering some of the alternative datastores popping up under the NoSQL umbrella - CouchDB and MongoDB on the document database side, and Redis on the key-value store side of things. I’m also going to have a play with the very latest Rails 3 code, so look for a few articles both on building a new app with Rails 3, as well as migrating existing apps.
And if anyone has any requests for intermediate or advanced topics they’d like to see me cover, then let me know. In the meantime, even if you’re an expert Rails developer, it’s still worth checking out the Ruby on Rails Tutorial book and helping Michael out with any feedback you might have.
Announcing Appsta 1.1.0
Worked a bit on tidying Appsta up this evening, and made a few decisions that will hopefully make it a bit more useful, and also hopefully get a few more people interested in using and contributing to it!
Appsta is at its core a library of helper methods for use in Rails templates, to help you build useful Rails app creation scripts that can cut down the amount of time it takes for you to get a new Rails app up and running with your standard setup. Everyone makes some initial modifications every time they break out the “rails” command, and by encapsulating this in a Rails template, you can save some time. Appsta added helper methods for setting up projects on GitHub, and setting up hosting environments on Heroku, amongst other things.
Appsta 1.0.0 also included a command line tool that you could use instead of the “rails” command, and used Appsta and a standard template to do some common things, namely setup staging and production Heroku environments, push the code to GitHub, setup some common gems, and use jQuery instead of Prototype/Scriptaculous. However, I realised over time that although that was a great default script for me, the whole point of Rails templates and advanced customization means that it probably wasn’t as useful for everyone else, as we all have slightly different preferences.
I decided therefore to do away with the command line “appsta” command and default template, so that Appsta is concentrated on simply being a library of useful setup helper methods for throwing into your own custom Rails templates.
It makes sense for developers to have their own customized Rails template with their own personal preferences for Rails application configuration (or perhaps per-company templates) - however it doesn’t make much sense for us all to have mammoth scripts that duplicates basic functionality within the template itself. The idea with Appsta is that over time we can accrue quite a large library of a variety of methods, and then building a customized template is just a case of picking and choosing the bits you want.
So Appsta 1.1.0 does just that, and tonight as well as tidying it up to remove the command line app, I also added in a shortcut for doing a git push, and helpers for removing some default and often unnecessary default files (README, public/index.html, public/favicon.ico), and also for removing the default JS libraries and using jQuery instead. An example template using all of the current available helper methods is on the wiki for the project on GitHub, but in short…
To install Appsta:
sudo gem install appsta
And to load Appsta in your Rails template, add in:
# Load Appsta
require "appsta"
Appsta.load
To remove default files:
# Remove the default README, public/index.html, and public/favicon.ico
remove_default_files
To setup jQuery:
# Remove the default JS libs and use jQuery
use_jquery("1.3.2")
And to push to Git remote(s):
# This performs a push of master to the specified remote(s)
git_push(:origin, :staging)
That covers the new bits, you can check out the Heroku and GitHub integration and the existing helper methods on the wiki page. I’m hoping that with the command line and default template clutter out of the way, Appsta can now be focused on reusable, useful chunks of helper code for your Rails templates. If you want to discuss this further with me, or have any comments or questions, please let me know. And if you want to contribute, don’t hesitate to fork the code, make your changes and send me a pull request!
Getting started with Rails 2.3.5
This is a tutorial guided towards those who are new to Rails, or those who have not used it since v1.x and who might want a newbie-style refresher. I’ll be working my way through to some more advanced and interesting topics soon, so for those of you who know all of this already, please stay tuned!
I’ve now been using Ruby on Rails for almost four years, and have seen it progress from version to version. In doing so, I’ve kept up with the changes between each version, and the new features that updates have brought to the table.
For newcomers though, the scene is a little different. Much of the information out there pertains to older versions, from really out of date stuff related to Rails 1.2, to things that may or may not still work as expected when talking about Rails 2.0 features.
I figured that even though it will one day be outdated as well, some Rails newbies might well find it useful to have a straightforward, simple up to date getting started guide for Rails 2.3.5. I’m going to do just that, and a few other articles on the Rails basics, before getting into some more advanced stuff. This article will start specifically dealing with setting up an app, before building our first REST resource using scaffolding.
To begin with, let’s install Rails. I’m assuming you have Ruby and RubyGems installed, but if you have issues with the base setup on Mac OS X, then there is an excellent tutorial here. Once you’re all setup, install Rails as follows:
sudo gem install rails
This will install the latest version of Rails, which at the time of writing is 2.3.5. To specifically install version 2.3.5, for example if 2.3.5 is no longer the most recent version, use this instead:
sudo gem install rails -v 2.3.5
If you don’t want to install local ri and RDoc documentation (i.e. you’ll be looking up API docs online, for example), then you can save a little disk space and a bit of time on installation by doing:
sudo gem install rails -v 2.3.5 --no-ri --no-rdoc
That works for any gem installation.
Once it’s installed Rails 2.3.5 and it’s dependencies, it should leave you with the “rails” command available from the terminal. If you already had a previous version installed, you can make sure that you’ve got the right version at your fingertips by running:
rails -v
That should print “Rails 2.3.5” to the console.
Now on to the exciting part; to create an application, it’s as easy as running:
rails <appname>
So for example:
rails my_new_test_app
I’ll be going over a new feature, Rails templates, and how they can speed up your typical Rails application creation process, in a future post. For now, the default application skeleton created by that command will do.
Let’s go ahead and test our new application. Change into the directory for the new app, and fire it up.
cd my_new_test_app
ruby script/server
This will fire up a development server on port 3000 by default, and by visiting
http://localhost:3000
in a browser, you’ll be able to see the app running. You should be greeted with the default Ruby on Rails page.

Let’s do something a little more exciting, and build something interactive. Rails uses some sensible configuration decisions out of the box, and also provides a fair few generators to get you going with some code. We’ll use one of these generators to provide what’s called a “scaffold”, an automatically built representation of an entire resource, giving you everything you need to get something going quickly. In time, the scaffolding will become less useful as you get used to building the resources yourself more quickly to suit your exact requirements - however to get up and running really quickly, it’s a great start.
We’re going to build a basic note resource, allowing the creation, retrieval, update and deletion of notes (these four actions are referred to as CRUD). To scaffold the resource, run:
ruby script/generate scaffold note body:text
This basically is creating a note model with accompanying controller and views, and we’ve also told it that it should have a single column - body, a text field. It will also be given some timestamp columns (created_at, updated_at) which are updated automatically and can be very useful.
Now let’s go ahead and run the command to bring the database up to date with our changes:
rake db:migrate
This will setup the notes table that was created as part of the scaffold. Now if we visit http://localhost:3000/notes, we can see our fully working notes resource in action!

You can play around with creating, updating and deleting notes and see that it all works straight off the bat.
If we revisit http://localhost:3000 though, we still see the landing page. Let’s set it up so that the root of the site displays the notes index page. We need to do a couple of things to make this happen. Firstly, we need to remove that placeholder landing page, so run:
rm public/index.html
However this still leaves you with the following error when accessing your app now:

We now need to tell the application to use the notes resource for the root of the site. Rails routing can be as complex or as simple as you need it. If you open up config/routes.rb, you’ll see that at the top there is the line:
map.resources :notes
This is the line that exposes your notes resource at http://localhost:3000/notes. So how do we hook up the root of the site to hit the index page for the notes resource? We just need to add a single line to the top of our config/routes.rb file. Right below this line:
ActionController::Routing::Routes.draw do |map|
add in the following line:
map.root :controller => :notes, :action => :index
This tells the app that the root URL (/) corresponds to the index action of the notes controller, which is the one responsible for displaying the list of all notes (the same as seen at http://localhost:3000/notes). Now if you reload http://localhost:3000, you’ll see that it shows the same content as http://localhost:3000/notes.
As before, you can play with the notes to see that it all works correctly - it doesn’t look too pretty, and is almost rarely exactly what you want out of the box, but when getting started with Rails it can provide an exciting and quick way to get new things up and running quickly, and to toy around with.
Now that we’ve got a basic, working application running on Rails 2.3.5, we’ll draw this article to a close. In future articles we’ll delve into more detail on some of the configuration choices you can make when setting up your application, some further detail on building out REST resources, testing your application, and some other more advanced stuff.
For a more in depth and lengthy look at getting up and running with Rails, check out the Rails guide for Getting Started with Rails.
As usual, if you have any comments, queries, suggestions or questions about the above, please get in touch!
Ready to Rumble
Wow, so what a 48 hours it was doing the Rails Rumble! Really great to get something up and running in two days. Me and my boy Mike really got it hooked up and came up with something pretty cool we think.
Likis is a language wiki designed to encourage collaboration and contribution to build an extensive language resource, to help people learn languages. It allows wiki-style editing of language pages, phrases and words, and it also includes audio pronunciations, so you can upload a recording of a specific foreign language word or phrase to help others!
Check it out, and let us know what you think! Now I need to get some sleep…
Next page »