Improve Performance With Record/Resource Caching
For my personal homepage I wanted to load the latest tips from Handy Rails Tips as well as my most recent Tweets on twitter. As these resources are stored on different sites from my homepage, there was a little lag when loading the landing page.
I don’t tweet or post tips every day so it seemed a little unnecessary to have these loaded fresh with every hit to my s
Faster Tests With factory_grabber
I use factories instead of fixtures when testing. If you haven’t already discovered factories, check out this Railscast: Factories not Fixtures.
Now that you’re up to speed: I often find when writing tests that I simply want any record. Here’s a quick example scenario from a controller spec:
describe CommentsController do
Make Your Life Easier - Write Less CSS
As far as programming languages go, CSS is the least fun to write. Without variables, mixins or mathematical operations, it can be very time-consuming to write CSS and a nightmare to make changes.
Introducing… Less: Leaner CSS.
Less is a gem that helps alleviate the pain of writing CSS by making it DRYer and far more intuitive.
To install the gem, simply run:
<rht
Dynamic Page Caching With Prototype
Ryan Bates recently released a Railscast on Dynamic Page Caching. This technique caches each page and then updates the page’s dynamic content using javascript.
If you haven’t seen the screencast, check it out you read on: http://railscasts.com/episodes/169-dynamic-page-caching.
DRYing Up Your Code With A Little Metaprogramming
An introduction to Metaprogramming
Metaprogramming in a nutshell is writing code that writes code. Here’s a really simple example:
<% 100.times do |number| %> <%= content_tag :strong, "hello" %><% end %>
Will output:
helloa hundred times.
This really simple example illustrates the power of metaprogramming; in theory, i
Writing A Rails Application With TextMate
For Mac users, TextMate is powerful tool for writing code. But having a great tool is pretty pointless unless you know how to use it. By spending a few hours learning about TextMate’s features you can almost half the time it takes you to write code. This tutorial will walk you through creating a simple Rails app using some of TextMate’s more advanced features.
Before you start, I’d recomme
Knowing When To Use flash.now
The flash hash is designed to carry a message from one action to the next. Here’s a simple example:
def create @user = User.create params[:user] if @user.save flash[:notice] = "User account created - Sweet!" redirect_to @user else render :action => "new" end endYou’ve probably seen examples like this a hundred times. A new user is created, the
Drying Up Your Ruby Code With Modules
One of the fundamental principles in Ruby is DRY; Don’t Repeat Yourself; which makes sense. Why waste time and effort re-writing code when you can simply reuse code you’ve already written? It also means if you ever need to change your code, you only have to change it in one place.
When writing an application you’ll often find there are methods that are the same in two or more of your m
Adding A Close Button To Your Flash Messages
In tip #3 I showed a quick javascript tip to hide your flash messages after a time delay. This technique may not be suitable for every application however; hiding feedback messages too quickly may make your site less accessible, especially to users who are visually impaired.
Here is a technique that allows your
Documenting your application or plugin using RDoc
When working with rails, the Rails Framework Documentation and
Ruby Documentation are invaluable resources.
With RDocs like these, you can quickly find information on classes, modules, methods etc. and also check out the source-code to help you better understand the code you are using.
Here is a quick tutorial to help yo
Some Handy rake Tasks
Here are some handy rake tasks you may not be aware of and a quick description of what they do:
Database tasks
rake db:create – Will create a database to the settings defined in database.yml for the current environment.
rake db:create:all – Will create all of the databases defined in database.yml
rake db:drop – Will drop the database for the current environment.
*r
Leave Yourself Notes With Annotations
When working on your apps, it’s really easy to lose track of the things you still need to do.
You’ll find there are tons of occasions where you spot something that could be refactored, changed, improved, sorted etc. but you may not be able to deal with it right away.
Rails 2 introduced a really cool new feature called annotations so you can leave notes for yourself in your code and
Freezing Your Rails App and Unpacking Gems
Rails is constantly evolving. Although each new version of Rails is (usually) an improvement on the last, upgrading to the latest version can sometimes lead to annoying problems with your existing apps. For example, when upgrading to Rails 2.3.2, you may have found your earlier apps raise exceptions due to application.rb being re-named to application_controller.rb.
If you’re running
Preventing Users From Taking Reserved Names
If your app allows members of the public to sign up and create a new user account, to post comments or disussion entries, it’s important to maintain some control over the information they can enter.
Leaving usernames like admin, webmaster, superuser or your domain name etc. open for the public to use can be pretty damaging if they are misused my a malicious user. For your own p
Playing MP3's On Your Rails Site With mp3_player
mp3_player is a simple plugin I built to play mp3s online through my rails application.
The plugin in based on a php/flash based plugin by Martin Laine
(This is how it looks while playing)
To install t
Testing Your Mailers Using Cucumber and Email-Spec
If you haven’t yet discovered Cucumber, check out these Railscasts:
If you’re already up to speed with the basics of Cucumber, here is a really useful way to test emails using Cucumber.
First we need to install the email-spec plu
Styling Your Blog's Code To Look Like Your Favorite Text Editor With CodeRay
For those of you out there with your own blog who want to style your snippets of code to look like your favourite text editor, here is a simplified version of the method I’ve used here on Handy Rails Tips.
To do this we need to employ CodeRay, a really cool gem for syntax highlighting by Kornelius Kalnbach. To try it out, add the following to your environment.rb file:
config.g
Loading Observers Automatically From Their Own Folder
Observers are a great way of cleaning up and DRYing up your models. I use them quite a lot to deal with sending mails, logging messages and anything else that isn’t strictly the model’s responsibility.
One thing that’s bugged me since I discovered observers is that they are not invoked by default. Instead you have to explicitly state which observers should be loaded (usually in environme
Hiding the Flash Message After a Time Delay
The flash hash is a great way to provide feedback to your users.
Here is a quick tip for hiding the flash message after a period of time if you don’t want to leave it lingering around.
First, add this line to the head of your layout to ensure the prototype and script.aculo.us javascript libraries are loaded:
<%= javascript_include_tag :all %>Next, add the fo
Managing your log files from within your app.
The log file can offer valuable information about what’s going on on your app.
If your app is running on a shared server, if it uses a lot of ajax or if you’re lucky enough to have lots of traffic to your site your log can get pretty huge pretty quickly and take up valuable disc space.
One way around this is to configure Rails to only log messages with severity level ‘warn’ or higher
Keeping Your Dates and Times DRY with to_formatted_s
Raw dates and times in Ruby are not too user-friendly!
The default times and datetimes in rails are pretty unattractive and contain more information than we usually need to display.
For example:
A useful way around this is to use the strftime() method to specify the format of the date or time.
So instead, we can use:
<ru