Recent Blog Posts
Blogs » Ruby on Rails
Get on Track
: Ruby on Rails BlogOver One Terabyte of FREE PDFs!
Our hugely popular Ruby On Rails free PDF giveaway has resulted in us serving up over 1 terabyte of free PDF files!
Since the initial announcement that our beginner’s Rails book, Build Your Own Ruby On Rails Web Applications, by Patrick Lenz, would be made available in its entirety in PDF form for FREE, close to 50,000 people have downloaded the book.
Normally retailing for USD $29.95, the book has been completely updated with all reported errata, and includes an entire new section on debugging Rails apps using the ruby-debug tool.
Due to the large number of simultaneous requests, several people experienced problems downloading the book in the first couple of days. We immediately moved the file to Amazon’s S3, which handled the enormous load much more gracefully.
If you were one of these people, please try again now — this amazing offer is only good until mid-November!
Free PDF Giveaway Takes Down SitePoint
The SitePoint file server crashed overnight because of a massive influx of traffic due to our FREE download offer of “Build Your Own Ruby on Rails Web Applications”. In fact, we were getting over 200 simultaneous download requests when the server crashed!
We’ve now setup a copy of the eBook on Amazon’s S3, ensuring that we can handle many more download requests at the same time.
Grab your free copy of our Ruby on Rails Book today and spread the word!
SitePoint’s Ruby on Rails Book is now FREE
I’m very happy to announce that for the next 60 days our book Build Your Own Ruby on Rails Applications is FREE* in PDF Format (a $29.95 USD value).
That’s right… Not an extract… Not a sample chapter… The ENTIRE 447 pages of Patrick Lenz’s incredible book is (for the next 60 days) free to download.
From installing Ruby, Rails and MySQL, to building and deploying a fully featured web application, this book has it all. Imagine building a Web 2.0 social news application, while learning the ins-and-outs of Ruby on Rails. This book shows you how to do it, step by step …
It’s no wonder this book has been described as the best Ruby on Rails beginners’ book on the market.
If you’ve ever thought about trying out Ruby on Rails, you’ll never get a better chance to learn why everyone’s talking about this revolutionary web development framework. Grab your FREE download today …
Feel free to spread the word to anyone who may be interested in Ruby on Rails, or anyone who just likes getting stuff for free :)
By the way, if you prefer your books in hardcopy …
Rails 1.2: No More Excuses!
In case you’ve been living on another planet and missed it, this month saw the release of Rails 1.2, a major milestone for the framework.
To coincide with this release, SitePoint this week announced the availability of our own Ruby on Rails book, Build Your Own Ruby On Rails Web Applications by Patrick Lenz. This is a terrific addition to the SitePoint library, written specifically for Rails 1.2. If you’ve yet to discover what the fuss is all about (and why Rails has web developers so excited), then you’ve got no more excuses not to take the plunge!
For all its strengths, though, Rails can be daunting for beginners, as coming to grips with the framework requires also learning a set of skills and concepts associated with best practice web development.
Beginning programmers will therefore appreciate the attention that Patrick gives to core topics such as the model-view-controller pattern, migrations, and continuous testing, as well as his explanations of object oriented concepts and in-depth coverage of Ruby syntax.
As technical editor of the book, I’m also very proud of the attention to detail applied to the book–in …
The State of Web Development: PHP developers most likely to switch to Rails
Of all the graphs in The State of Web Development 2006/2007 report that SitePoint and Ektron published recently, the one that stood out the most to me was the one showing the platform currently being used by developers planning to start using Ruby on Rails:

It wasn’t the fact that the graph shows PHP developers to be likely to start using Rails — this makes sense, as someone is more likely to experiment with another open source platform if they’re already using another open source platform — it was the fact that PHP developers are more than three times as likely to start using Rails, even more so than Java developers!
This surprised me, because it’s the Java developers that the Rails movement is targeting (note: purely my own observation, based on the Rails tagline of being able to write “real-world applications with joy and less code than most frameworks spend doing XML sit-ups”). To a Java programmer, Rails might well look like a much simpler way to achieve the same …
JSON-P output with Rails
At the recent Web Directions 06 conference Cameron Adams and Sitepoint’s own Kevin Yank gave a talk on Mashups and APIs, with a mention of JSON-P.
JSON-P is a method of wrapping the JSON output of your API calls to allow other developers to call your API from within their page, bypassing the browser security mechanism.
If you peek at the HTML source of Web Connections you’ll notice we’re using JSON-P internally to provide the JS data to do the mapping. In the URL of the JSON-P call we can specify the parameter variable, callback or both, and this will allow others to use our JSON in their own mashups.
It also integrates nicely with the new Rails REST functionality. For example, Jeremy Keith’s profile page can also be output in JSON format by plugging ‘.js’ on the end. This is nice, but not very useful if you wanted to call it from another page.
Say Jeremy wanted to mashup this data on his own website to show who he’s friends with. He’d need some kind of JSON-P output so he can access the data from a script tag.
To add the JSON-P “padding” it’s just a matter of …
Roll your own dispatch reaper script
If you haven’t used it before, the reaper script is useful for restarting your Rails application’s FastCGI dispatchers.
It’s seems the reaper script in Edge rails now relies on the existance of PID files in the tmp/pids directory. If you want to replicate the old functionality, create your own ruby script and override the capistrano restart task to call your own script instead of the standard script/reaper.
The reaper script for one of my apps is simply:
#!/usr/bin/env ruby
for process in `ps axwwl`.grep(/dispatch\.fcgi/).collect { |s| s.split[1] }
puts “Sending USR2 to dispatch.fcgi process #{process}”
Process.kill(”USR2″, process.to_i)
end
The above code is in script/my_reaper and is called from the Capistrano deployment recipe as below:
task :restart, :roles => :app do
run “ruby #{deploy_to}/current/script/process/my_reaper”
end
Being a good little 404er
Using ActiveRecord’s RecordNotFound exception to cleanly and consistently handle 404s throughout your application
Timing out and retrying calls to third parties
When calling third parties you want to set them a sensible timeout, and you also might want to make a few attempts before giving up. Here’s a relatively succinct way to accomplish this in Ruby (3 attempts, each with a timeout of 5 seconds):
3.times do
begin
Timeout::timeout(5) do
# Call your third party (for a example, a payment gateway)
end
@success = true
break
rescue Exception
# Try again!
end
end
if @success
# Jump for joy!
end
I’m sure somebody will chime in with an even sexier way…
The effect of using Rails fragment caching
Rails provides a few caching techniques, one of which is a fragment cache. The fragment cache is a dead-easy way to cache bits of your view.
After checking that your queries are doing sensible things, the next often slowest part of the Rails stack is view rendering, especially if there’s lots of URL generation involved. More often than not you don’t want to cache the entire page as static HTML and bypass Rails altogether, instead, you want to retain the dynamic goodness of a web app and just cache the slow bits.
I recently added fragment caching to the toolmantim.com home page. Before I added fragment caching my production.log looked a little something like this:
Completed in 0.12385 (8 reqs/sec) | Rendering: 0.10077 (81%) | DB: 0.02210 (17%) | 200 OK [http://toolmantim.com/]
After fragment caching 2 parts of the application layout and the body of the home page my production.log looks a little something like this:
Completed in 0.00615 (162 reqs/sec) | Rendering: 0.00583 (94%) | DB: 0.00000 (0%) | 200 OK [http://toolmantim.com/]
A 17 times speed increase in rendering time for the cost of 2 dead-simple cache sweeper models, 4 lines of controller code and 6 lines of view code. I’d say that was …
Sponsored Links
SitePoint Marketplace
Buy and sell Websites, templates, domain names, hosting, graphics and more.
Download sample chapters of any of our popular books.




