Archive for the ‘Ruby on Rails’ Category

Ruby on Rails Beyond the Basics

Thursday, October 18th, 2007

RoR BTB package

I’m happy to announce that my latest video training, Ruby on Rails Beyond the Basics, has been released on the lynda.com Online Training Library.

Ruby on Rails Beyond the Basics is the sequel to Ruby on Rails Essential Training and is 11.5 hours of video training for web developers who already feel comfortable with the fundamentals of Ruby and the Rails MVC framework. I teach more of the Ruby language and spend a lot of time on code blocks. Then, using the example of a blog as a project, I show you how to use migrations, demonstrate how to create CRUD that doesn’t simply mimic scaffold, and give you a better understanding of ActiveRecord and some of the pitfalls that often trip up developers. I also cover validations, callbacks, routes, pagination, performance improvements and more.

Ruby on Rails Beyond the Basics is available online as streaming video to subscribers of the lynda.com Online Training Library ($25/month, $250/year, $375/year with exercise files included). It is will also be available as a CD-ROM soon both through lynda.com and Amazon.com.

If you are a beginner, you will be better off if you first start with Ruby on Rails Essential Training which is available online to lynda.com OTL subscribers and on CD-ROM, both through lynda.com and Amazon.com, for $99.95.

If you try either of them and find that they are helpful, be sure to let me know!

Topics include:

  • Updating Ruby, Rails and projects
  • Using irb and the Rails console
  • Intermediate Ruby language techniques
  • Understanding code blocks
  • Catching errors and raising exceptions
  • Using migrations to manage database changes
  • Understanding ActiveRecord and avoiding common pitfalls
  • Creating flexible controllers and views
  • Improving performance with eager loading and cache counters
  • Working with form validations and model callbacks
  • Using partials and helpers to organize views
  • Salting passwords for increased security
  • Choosing how to store session data
  • Understanding and defining custom and named routes
  • Pagination
  • Using plug-ins

Rails version 1.2.5 and Capistrano 2.1

Monday, October 15th, 2007

If you blinked you would have missed version 1.2.4. Rails version 1.2.5 has been released. For the most part, it is a security release to fix a JSON problem. If you aren’t using JSON, then it’s optional but never a bad idea.

If you didn’t upgrade to version 1.2.4 already, don’t be alarmed when gem install rails –include-dependencies downloads version 1.2.5 now.

If you want to upgrade to 1.2.5, you can follow the same steps as I outlined previously for 1.2.4.

And Capistrano 2.1 has been released.

Capistrano is a tool for automating tasks on remote servers, especially those running Ruby on Rails applications. It lets system administrators execute commands and roll back changes in parallel across multiple machines.

Rails version 1.2.4

Thursday, October 11th, 2007

As foreshadowed last week, Rails version 1.2.4 has been released. It’s mostly made up of security fixes and deprecation notices that will help you prepare for Rails 2.0.

Steps to Upgrade

Install Rails 1.2.4

Install the Rails 1.2.4 gem on your development server. You could install it by freezing a version into your vendor directory (like I recommended for Rails 2.0) but honestly, this release is minor and I recommend you simply upgrade the gem instead.

1
2
3
gem install rails --include-dependencies

#(or sudo gem install rails --include-dependencies)

You’ll end up with both your old Rails version and version 1.2.4 as gems.

1
2
# display all installed gems with:
gem list --local

You have a few choices on what to do with the old versions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Option 1: uninstall all gems which have a newer version
# (generally the best option after you are ready to commit to 1.2.4)
gem cleanup

#You can preview what cleanup will do with:
gem cleanup --dryrun

# Option 2: uninstall only the old Rails version
# (use if you need to keep old versions of other gems around)
# list the gems if you think you have different versions
gem uninstall rails --version 1.2.3
gem uninstall actionwebservice --version 1.2.3
gem uninstall activerecord --version 1.15.3
gem uninstall actionmailer --version 1.3.3
gem uninstall actionpack --version 1.13.3
gem uninstall activesupport --version 1.4.2

# Option 3: uninstall the new Rails version
# (only if something did not work out and
# you need to revert to your old version)
gem uninstall rails --verison 1.2.4

Track Down Deprecated Code

There are three ways that you can track down and update your deprecated code. I recommend doing as many of them as you can.

1. Run your application’s tests to see the deprecation warnings.

2. Click through your application by hand and watch the development log for the errors.

3. Save this code by Mislav Marohnić as “r2check.rb” and run it from the root of your application. It will scan through your application files searching for matches to regular expressions corresponding to deprecated code. (You won’t need to explicitly specify the location of your application—it will assume it is the current directory.)

For example:

1
2
3
cd /path/to/your_rails_app

ruby /path/to/r2check.rb

Watch Out for Previously Deprecated Code

You’ll also want to make sure you’ve taken care of all previous deprecations. I’ve listed a few key ones below.

  • @params, @sessions, @cookies, @flash, @request, @response, @headers, @env, and @template have all been deprecated in favor of simply using params, sessions, cookies, etc. without the @ prefix. (It’s a method call that returns an instance variable instead of an instance variable directly.)
  • @content_for_layout has been deprecated. Use yield instead.
  • Forms and links performing POST operations should use :method => :post instead of :post => true
  • start_form_tag and end_form_tag are outdated. Use form_tag() do {…} or form_for() do {…} instead.
  • render_partial has become render(:partial => ’something’)
  • find_all and find_first have become find(:all) and find(:first)

Install Pagination Plug-in

Pagination will be moving to a plug-in. If you have been using pagination anywhere in your application, you will want to install the plug-in for either ClassicPagination or change your code to use WillPaginate. (Both plug-ins can co-exist too if you want to start with ClassicPagination and move to WillPaginate over time.)

1
2
3
# Note: you'll need to have Subversion installed
ruby script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination
ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

And then you’ll be in a great position to upgrade to Rails 2.0 once the final release comes out later this month!

UPDATE: I forgot to explicitly provide the final steps. Once you have the upgrade installed, go into your Rails application. Open up config/environment.rb and update the RAILS_GEM_VERSION to ‘1.2.4′ or ‘1.2.5′ (most likely on line #8). Save and close the file. Then from a command line from the root of your Rails application, run rake rails:update:configs. (First making backup of that directory is always the safest choice.) That will make sure your configuration files (like boot.rb) get updated with the latest code too.

Rails 2.0 Preview Release

Tuesday, October 2nd, 2007

Rails 2.0 PR

The Rails 2.0 Preview Release is out. David Heinemeier Hansson provides an overview of some of the new features. There may be several preview releases before the final release. I’m sure there will be bug fixes—but DHH said in his RailsConf Europe keynote that 37signals has been running many sites (including Basecamp) on most of the Rails 2.0 code for several months, so it ought to be pretty solid by now. If I had to guess, I’d say we are 3-4 weeks away from a final release.

Before the final release, the Rails core team have also promised to release version 1.2.4. The update to Rails 1.0 will likely include minor bug fixes, however it’s primary purpose will be to provide complete deprecation warnings. So the process will be: upgrade to 1.2.4, try everything, watch the log files for deprecation warnings, fix any incompatible code, then upgrade to 2.0. Don’t be surprised if 1.2.4 is released only a week before 2.0.

The best way to safely try out the preview release is to download it into the vendor directory of a Rails project. (You will need Subversion installed. If you are on a Mac, this guide can help you install it.) Using a command line, navigate into the root of the project and type:

1
rake rails:freeze:edge TAG=rel_2-0-0_PR

Once the download is complete, that Rails project (and only that project) will be running on the preview release. Type rake rails:unfreeze if you want to go back to your regularly installed Rails version. Advanced users can opt to install the gem version of the preview release instead.

UPDATE: If you are on Rails 1.2.3, you’ll need to run the “freezing” twice. The first time will not download the new ActiveResource pieces (Rails 1.2.3 doesn’t know about them) and you’ll get errors when you try to start the server. When you run it the second time, Rails will know that it should have ActiveResource and will retrieve it for you.

RailsConf Europe

Wednesday, September 26th, 2007

RailsConf Europe took place last week in Berlin. Below are the links to speaker notes and reviews that I’ve collected.

Speaker Notes

Official Slide Summary

Reviews and Commentary

Let me know if you know of others and I’ll post them too.

Rotating Rails Log Files

Monday, September 10th, 2007

Logrotate

One of the chores I dislike most is cleaning up and clearing out the log files in my Rails applications. On some of my higher profile sites, I get a lot of spiders probing my applications for security holes. They don’t succeed (knock on wood) but they do fill up my log files with errors.

I finally decided to get smart and get lazy (the two best traits a programmer can have), and I set up automatic log rotation on all of my Rails applications. The idea behind log rotation is simple: make a back up of the current log file, continue logging into a new or cleared log file, and discard log files that are older than a certain date.

Your webserver probably already rotates its own log files. For Apache, they are probably located in /etc/httpd/logs and they are probably rotated weekly. These logs store everything Apache does. Simple webserver stats and traffic analysis tools make use of these log files to show who visits a site when and what pages are viewed.

While it is possible to configure your Rails application to log to your Apache log files, I do not think it is a good practice. It’s much better to give each Rails application its own log file—it will be easier to find important Rails errors, it will keep your Apache logs cleaner and Rails is set up to keep its own logs by default. Fortunately, on a Linux server the built-in logrotate program will make the process super-easy. After the jump, I’ll walk you through the steps to get it set up.

(more…)

Post-Vacation Link Dump

Tuesday, September 4th, 2007

Here’s some of the exciting things I didn’t blog about while I was on vacation last week.

Let’s Get Ready to RailsRumble!

Monday, August 13th, 2007

RailsRumble Badge

The First Annual Rails Rumble Championship has been announced.

Rails Rumble is a competition to test your Ruby on Rails hacking skills by building a brand new web application over two days—September 8-9, 2007. Your team, consisting of up to four people, gets just 48 hours to design, develop, and deploy a web application from scratch. The winners of the Rumble will be judged by the Rails community. There will be prizes and Ruby East will showcase some of winners at their conference.

Not only does it sound like fun, but this could be a good opportunity to dig up those back-of-the-envelope and cocktail-napkin ideas that you’ve been saving and turn them into a real web applications (while getting great exposure). I’ve definitely got a few of those lying around, but unfortunately I’m booked that weekend.

I think it would be cool to see coders who don’t know each other collaborate. I also think it would be sweet to have teams grouped around a common idea: maybe four women coders willing to show up the boys, or four coders interested in applications for charaties or non-profits, or coders from four countries putting together an international application, or four college kids improving the student loan experience… You get the idea.

Don’t have a team of four? You can probably find other solo-programmers by posting in the comments to this post or via the Ruby on Rails list.

Who knows? Maybe your quartet will become the founders of the next killer web app.

Atom Publishing Protocol

Tuesday, August 7th, 2007

Atom Logo

The Atom Publishing Protocol is now a real web standard (RFC 4287).

APP is different from, but closely related to, the Atom Syndication Format which is an XML format used for web feeds and an alternative to RSS feeds. The Atom Publishing Protocol goes a step further than syndication and uses XML-formated requests over HTTP to manipulate Web resources. In other words, it’s not just for reading, it allows for creating, reading, updating and deleting (CRUD) resources over the web.

Those of you following the development of Ruby on Rails will recognize APP as being an example of a RESTful protocol. (The REST model is one of the new features of Rails.)

With it’s ability to edit data and support for arbitrary media resources, Atom becomes useful for web feeds, wikis, calendars, photo libraries, podcasts, video distribution, document management… even software distribution. You can publish anything to the world and the world can interact with it!

The days of complicated web services like SOAP and WSDL are behind us. Atom and REST are on the rise and I’m interested to see what developers do with them. For example, there’s been a lot of discussion recently about opening up closed social networks like Facebook (here, here, here and here). Atom/REST could allow a simple way to share information between social networks. An update to your profile or friend list on one social network could ripple out to all of your others using Atom.

A good place to start learning more about Atom is Atom Enabled.

The Monthly Bug

Wednesday, August 1st, 2007

Monthly Bug

I stumbled upon an odd Ruby on Rails bug this week. It was difficult to track down because it was a bug that only affected some users some of the time. Out of every 100 users: 50 would have no problems, 42 would have problems 2% of the time, 8 would have problems 8% of the time. Making it more difficult to pinpoint—during 92% of the year none of the users experienced any problems at all! Let me demonstrate the problem and explain how to solve it.

Let’s say we have a form for a user to fill out. On that form, we want the user to provide a date, but we don’t care about the day of the month; we just want the month and year. We might be asking for their birth month and year or how long they’ve been using our product. The most common example would be asking for a credit card expiration date.

(more…)