Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"

Find geocoded records that are close to a location (radius search)

When you have objects in your database that hold latitude and longitude and you want to find others that are close to given coordinates you can use the Graticule gem.

Graticule

Graticule offers several methods to compute the distance between two geo-dated objects but fetching records from the database that are within a given radius of a location is a bit trickier:

def close_destinations(latitude, longitude)
  distance_sql = Graticule::Distance::Spherical.to_sql(:latitude => l...

Run a script on the server

You have to specify the environment with -e env_name or RAILS_ENV=env_name if you want to run a script on the server.

at Rails 2 it’s script/runner

bundle exec script/runner -e env_name path/to/script.rb argument1 argument2 ...

at Rails 3 it’s rails runner

RAILS_ENV=env_name bundle exec rails runner path/to/script.rb argument1 argument2 ...

Preparing your test database: mind the differences

Instead of running all missing migrations on your test database with rake db:migrate RAILS_ENV=test you can also use a handful of rake tasks to prepare the database structure directly. They can produce different results, though.

In a nutshell, to ensure your test database gains the correct structure:

  • Don’t use rake db:test:prepare carelessly
  • or use rake db:test:clone_structure ← preferred :)
  • or use rake db:migrate RAILS_ENV=test and don’t mix it with other ways, like some of the rake tasks.

rake db:test:prepare ————…

Login forms: Disable browser prompt to remember the password

In order to prevent the browser from asking whether to remember the password, give a form an autocomplete attribute with the value off:

<form "/session" method="post" autocomplete="off">
  ...
</form>

Rails example

form_for @model, :html => { :autocomplete => "off" } do |form|
External content

jQuery 1.7 Released

Two new methods on and off are the new way of declaring event handlers. bind, delegate and live area deprecated. Also better performance for delegated events.

"Show me the page" fails to open a browser window

If you get an error like this:

Unable to launch /home/bruce/Projects/myproject/tmp/capybara/capybara-201110311210111407691101.html 

… update your launchy gem. It failed for us in version 0.4.x. We could fix the issue by upgrading to 2.0.5.

"Require group" doesn't work with Ubuntu's default Apache installation

If you want to use Require group $GROUPNAME on your default Apache installation like this:

<Directory "/var/www/foobar">
    Order allow,deny
    Allow from all
    Options None
    AllowOverride all
    AuthName "Area 51"
    AuthType Basic
    AuthBasicProvider external
    AuthExternal pwauth
    Require group admin
</Directory>

You need to first install libapache2-mod-auth-sys-group. You don’t get any error message if you haven’t installed it but it doesn’t work.

sudo apt-get install...

Declare different CSS background-images for different locales

If you would like to use language specific layout (e.g. background-images) in your applications stylesheets you can achieve this easily by using the lang attribute in your views (ERB):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= I18n.locale || 'en' %>" lang="<%= I18n.locale || 'en'%>">
...
</html>

or in HAML:

%html :xmlns => "http://www.w3.org/1999/xhtml", :"xml:lang" => I18n.locale || 'en', :lang => I18n.locale || 'en'

Then, in your stylesheet you can for example declare different background-images fo…

See with tig which git commits touch a file or files or folders

tig path_to_file_or_files_or_path_with_wildcard

Large forms are slow on the iPad

  • Forms with many inputs (600+ in my case) become extremely unresponsive on an iPad, up to the point where it can take several seconds for a control to respond to touch commands.
  • This is true for both iPad 1 and iPad 2 models.
  • While certain CSS styles can lead to performance issues, removing those styles won’t help if the form simply is very large.
  • A workaround is to only show a limited number of form inputs at the time, e. g. by toggling groups of form…

Ruby: Downloading files

This is easy:

require 'open-uri'

File.open('/target/path/to/downloaded.file', "wb") do |file|
  file.write open('http://example.com/your.file').read
end

#Basic Authentication
When your file is protected by HTTP Basic Authentication, pass your credentials as hash:

File.open('/target/path/to/downloaded.file', "wb") do |file|
  file.write open('http://example.com/your.file', :http_basic_authentication => [your_username, your_password]).read
end

Rails 3.1 error message: Could not find a JavaScript runtime

After starting the Rails server in a freshly generated Rails 3.1 project you could see an error message such as

/usr/lib/ruby/gems/1.8/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Just add a JavaScript runtime to your Gemfile and the error vanishes.

Examples:

gem 'therubyracer'
gem 'extjs'

Solve: Unable to send PGP/GPG encrypted e-mail with Enigmail

So you changed your GPG key (e.g. because it expired) and Thunderbird won’t send any more encrypted e-mails with Enigmail?\
Your key is showing up in the key management window but does not seem to work?

Fix Thunderbird settings

If you get errors like “encryption failed: public key not found” your account settings point to the wrong key ID.

Fix it by navigating to: Edit → Account settings → Your account → OpenPGP Security. \
You will find the old key ID there which of course is no longer valid (and you may…

How to manage Thunderbird mailing lists

You can group contacts using a mailing list in Thunderbird for easier access and less trouble when trying to find out who to send a message to. Here is how to do this:

  1. Open your Thunderbird address book
  2. Press the “New List” button
  3. Choose a name for your list and add contacts. There will be auto completion from your address book.

When sending e-mails you can now add a recipient with the mailing list’s name and the e-mail will be sent to all its members.

state_machine: Test whether an object can take a transition

When using state_machine you sometimes need to know whether an object may execute a certain transition. Let’s take an arbitrary object such as a blog article as an example that has those states:

A -> B -> C -> D

Additionally, you have transitions between the states as shown above. Let’s call the transition between ‘A’ and ‘B’ transition_ab for simplicity. \
Given an object in state ‘A’. You can call object.transition_ab but calling object.transition_bc! will obviously fail because …

Be careful with "memoize"

ActiveSupport’s memoize has a dangerous feature you might not know about.

Assume you have

class DeepThought
  extend ActiveSupport::Memoizable
   
  def life_universe_and_everything
     # some lengthy calculation returning 42
  end
  memoize :life_universe_and_everything
end

Then #life_universe_and_everything will of course cache the result after calculating it once. However, you can trigger a recalculation by calling #life_universe_and_everything(true).

If, however, your method looks like

de...

Multi-line Ruby block in Haml

There are several options, but most of them are impractical. The best way is to use the :ruby filter:

:ruby
  puts javascript_include_tag(
    'lib/jquery-1.6.1.min.js',
    'lib/jquery-rails-ujs.js',
    'lib/jquery-ui-1.8.13.custom.min.js',
    'lib/jquery.ui.datepicker-de.js',
    'lib/jquery-ui-timepicker-addon.min.js',
    'lib/jquery.tools.min.js',
    'application.js',
    'google-maps.js',
    :cache => true
  )

Web font embedding requires new CSS for IE9

If you embedded web fonts in the past years (e.g. by copying CSS from a Font Squirrel @font-face kit), that CSS won’t work in Internet Explorer 9.

You can fix it by turning these styles…

@font-face
  font-family: 'MyFont'
  src: url('myfont.eot')
  src: local("☺"), ('myfont.ttf') format("truetype")
  font-weight: normal
  font-style: normal

… into these:

@font-face
  font-family: 'MyFont'
  src: url('myfont.eot')
  src: local("☺"), url('myfont.e...
2855 cards