dbconsole in Rails 3 requires the environment as the first argument

There is a bug in Rails 3's dbconsole script, which makes the following command open a database console for the development environment:

rails dbconsole -p test

You need to write this instead:

rails dbconsole test -p

Backup your Mac to an encrypted local hard drive

There are many blog posts on encrypting backups, but none works for local drives.

#How to

  • Encrypt the external backup drive using TrueCrypt or PGP or similar.
  • Mount it. If possible, let this happen automatically.
  • Tell Time Machine to use it for backup.

#What is NOT working

  • Backing up to disk images as described in this blog post. Apparently, sparsebundle images & co. ar...

When Balsamiq Mockups won't let you load an image file

Did you check Copy to Projects Asset as ... and there is an existing file with the same name in your project folder?

ActiveRecord 3+ auto-converts times to UTC by default. Hilarity ensues.

Remember how Rails 2 came with an awesome feature that broke all code using Time.now or Time.parse?

This behavior is now the default for Rails 3. Disable it by adding the following to your config/application.rb:

config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false    

Rendering a custom 404 page in Rails 2

Simple: Tell the application controller how to handle exceptions, here a RecordNotFound error.
Do this with the following line:

# application_controller.rb

  rescue_from ActiveRecord::RecordNotFound, :with => :render_404

This will call the method render_404 whenever a RecordNotFound error occurs (you could pass a lambda instead of a symbol, too).
Now write this method:

def render_404
  render 'errors/404', :status => '404'
end

Finally create a 404 document views/errors/errors.html.haml.

%h1 Record...

Let a Rails 3 application make a request to itself

Ever wondered how Rails talks to itself in a Cucumber feature? In Rails 3 you can do it like this:

def rack_env(path)
  { "rack.input" => {},
    "PATH_INFO"=>"#{path}",
    "REQUEST_METHOD"=>"GET" }
end

request = rack_env('/users/new')
response = Rails.application.call(request)
status, headers, body = response

puts status # e.g. 200
puts headers.inspect # hash of headers
puts body.body # html of response body

Instead of Rails.application you can also call any Rack application.

When Rails no longer renders changes in view templates or Sass stylesheets

Do you have page caching enabled for the development environment and there are cached pages lying around in public/?

Ruby: Convert a time string to your local time zone

If you have a time given in a different time zone than your local one, parsing will convert it for you:

>> Time.parse('September 2nd, 3pm PST')
=> 2011-09-03 01:00:00 +0200

Note that in pure Ruby you need to require "tzinfo" (Ruby 1.9) or require "time" (Ruby 1.8) for Time.parse to be available.

Fix: "undefined method `bytesize' for #<Array>"

I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:

"undefined method `bytesize' for #<Array>"

Starting the application in Passenger gave me a stacktrace in log/development.log that pointed to the actual problem.

Possible causes discovered by looking at the logs
-----------------------------------------------------...

Use different code for Rails 2 and Rails 3

When writing a piece of reusable code, you sometimes need to have separate code for Rails 2 and Rails 3. You can distinguish between Rails versions like this:

if Rails.version < '3' # mind the quotes
  # Rails 2 code goes here
else
  # Rails 3+ code goes here
end

Install the DeaDBeeF music player under Ubuntu Linux

DeaDBeeF is one of the better music players for Linux.

You can find installation instructions here.

Always show all form errors during development

You've been there: A form cannot be submitted, but you don't see a validation error because the field at fault has no corresponding input field on the form. Because this is usually a bug, you insert debug information listing all errors into the form view. And once the bug is fixed, you forget to take out that debug information.

There is a better way. By copying one of the attached initializers into config/initializers, your forms will always render a small box listing all form errors in the bottom right corner of the screen. This box is n...

Let Webrat make a POST request

Just add the parameter :post to the visit method:

visit publish_entry_path, :post

Webrat doesn't follow redirect because it considers the url external

Rails doesn't know which host it is running on. For generating links, it strips the hostname off the request URL, which can lead to errors when you have absolute URLs in your Cucumber tests.

If you really need to use absolute URLs somewhere, say in an email you send, either throw away the host when parsing it (e.g. body.scan(/http:\/\/[^\/]+\/([^\s"<]+)/)) or tell Webrat you're back on your site.

Timecop creates records in the past after calling Timecop.freeze

This is a bug in Timecop 0.3.4 or lower. You should upgrade to 0.3.5.

Time#utc, Time#gmt and Time#localtime are destructive methods

Calling Time#utc, Time#gmt or Time#localtime will not create a converted copy. Instead these methods modify the receiving Time object:

 >> time = Time.now
 => Thu Aug 25 09:52:28 +0200 2011
 >> time.utc
 => Thu Aug 25 07:52:28 UTC 2011
 >> time
 => Thu Aug 25 07:52:28 UTC 2011

This can have unexpected side effects when other code is holding pointers to the Time object you are modifying. To be safe, call these methods on a clone of the Time object. You can clone a Ruby object by using [#dup](http://...

Convert the colorspace of a PDF from RGB to CMYK under Ubuntu Linux

Note that converting from RGB to CMYK will usually degrade your colors because no exact mapping is possible. Anyway, this Stackoverflow post worked for me:

gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
-sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK \
-sOutputFile=output.pdf input.pdf

Rotate a PDF under Ubuntu Linux

Use the PDF toolkit:

sudo apt-get install pdftk

To rotate page 1 by 90 degrees clockwise:

pdftk in.pdf cat 1E output out.pdf    # old pdftk
pdftk in.pdf cat 1east output out.pdf # new pdftk

To rotate all pages clockwise:

pdftk in.pdf cat 1-endE output out.pdf    # old pdftk
pdftk in.pdf cat 1-endeast output out.pdf # new pdftk

The E (old pdftk) or east (new pdftk) is meaningful if you want other rotations. From the man page:

The page rotation setting...