Good real world example for form models / presenters in Rails

We have often felt the pain where our models need to serve too many masters. E.g. we are adding a lot of logic and callbacks for a particular form screen, but then the model becomes a pain in tests, where all those callbacks just get in the way. Or we have different forms for the same model but they need to behave very differently (e.g. admin user form vs. public sign up form).

There are many approaches that promise help. They have many names: DCI, presenters, exhibits, form models, view models, etc.

Unfortunately most of these approaches …

Manage your AWS credentials for multiple accounts

Create a directory mkdir ~/.aws

Initialise git repository cd ~/.aws && git init

Create a git branch with a name you want (e.g. development for the aws development account credentials).

Add AWS credential file .aws_credentials:


Also add your EC2 cert and private key file.
You can add other AWS account depending files like .fog or .guignol.yml too.

Create symlinks for some config files like .aws_credentials and .fog:

ln -s ~/.aws/.aws_credentials ~/.aws_cred...

OpenStack instance not configuring network (DHCP) correctly

We ran into trouble when adding additional compute units to our railscomplete Hosting environment lately.

VM-instances on the new compute units where booting and requesting private IP addresses via DHCP correctly (DHCPDiscover), but after the answer of the dnsmasq dhcp server (DHCPOffer) we did not see any further traffic on the host machine. FYI: The instance should request the IP via DHCPRequest which in turn should be acknowledged by a DHCPAcknowledgment packet.

We assumed this DHCP UDP traffic did not…

External content

Scaling Pinterest - From 0 to 10s of Billions of Page Views a Month in Two Years

Awesome battle report from Pinterest trying to scale their setup using different approaches.

When you push something to the limit all technologies fail in their own special way. This lead them to evaluate tool choices with a preference for tools that are: mature; really good and simple; well known and liked; well supported; consistently good performers; failure free as possible; free. Using these criteria they selected: MySQL, Solr, Memcache, and Redis. Cassandra and Mongo were dropped.

External content


jQuery plugin to fire events when user's cursor aims at particular dropdown menu items. For making responsive mega dropdowns like Amazon's.

Firefox >= 23 will block mixed content when using SSL

Non-SSL contents on SSL pages are blocked by default

Bug 834836 – Turn on pref to block mixed active content

Firefox 18 introduced preferences to block loading contents from non-SSL (http) sites on SSL (https) pages. One of those preferences, security.mixed_content.block_active_content is now enabled by default in order to enhance user security. That means insecure scripts, stylesheets, plug-in contents, inline frames, Web fonts and WebSockets are blocked on secure pages, and a notification is displayed instead. It will not block…

Git: How to show only filenames for a diff

When you want to do a git diff but do not care about the full diff and just want to know which files changed, use the --name-only switch:

$ git diff --name-only

To include some brief information about changed lines, use the --stat switch:

$ git diff --stat
app/controllers/sessions_controller.rb |    8 +-
app/models/user.rb                     |   30 ++++
features/sign_in.feature               |  136 +++++++++++++++++


Detect city, country from IP address

  • You can detect city and country from an IP address by using the GeoLite database. This is a flat file you can copy into your project (~ 20 MB).
  • You can access the database using the geoip gem.
  • You need to attribute MaxMind if you are using the data.
  • Accuracy sort of sucks. For most countries 1/3 of addresses cannot be resolved within 40 kilometers, probably because the Inter…

Tell RVM which patch level you mean by "1.8.7" or "1.9.3"

When you download or upgrade RVM it has a hardcoded notion which patch level it considers to be "1.9.3".

This can give you errors like "ruby-1.9.3-p392 is not installed" even if you have another Ruby 1.9.3 that will do.

The solution is to define an alias:

rvm alias create 1.9.3 ruby-1.9.3-p385

Fuzzy matching

Another solution is to use rvm with the fuzzy flag, as stated by mpapis.

rvm use --fuzzy .

This will make rvm more intelligent in the Ruby selection. To always do fuzz…

How to discard a surrounding Bundler environment

tl;dr: Ruby's Bundler environment is passed on to system calls, which may not be what you may want as it changes gem and binary lookup. For a bundler-free environment, use Bundler.with_clean_env. Do this whenever you want to execute shell commands inside other bundles.

Example outline

Consider this setup:

my_project/Gemfile     # says: gem 'rails', '~> 3.0.0'
my_project/foo/Gemfile # says: gem 'rails', '~> 3.2.0'

And, just to confirm this, these are the installed Rails versions for each of the bundles:


How to access your Rails session ID

This only works when you actually have a session ID (not the case for Rails' CookieStore, for example):

# => "142b17ab075e71f2a2e2543c6ae34b94"

Note that it's a bad idea to expose your session ID, so be careful what you use this for.

External content

parallel_tests: Disable parallel run for tagged scenarios

Note: This technique is confusing and slows down your test suite.

Copy the attached code to features/support. This gets you a new Cucumber tag @no_parallel which ensures that the tagged scenario does not run in parallel with other scenarios that are tagged with @no_parallel. Other scenarios not tagged will @no_parallel can still run in parallel with the tagged test. Please read the previous sentence again.

This can help when multiple test processes that access a single resource that is hard to shar…

Deal with different ways of counting weeks and weekdays in Ruby

Depending on where you live, different rules are used to determine the number of the week and a weekday. You have no chance whatsoever to get this right globally unless you make it your life's purpose. However, when you work for clients from Europe or the US, there are two dominantish standards you should know about. Each of these has subtle differences.

ISO 8601

  • This is adhered to by most European countries.
  • Weeks start on Mon…

When RubyMine doesn't show you Git blames / annotate gutter not available

  • You probably haven't configured version control for your project.
  • Go to Project Settings / Version Control and set the director <Project> to Git

Snom VOIP phone shows your own name instead of the incoming number

This is because of how Sipgate sends the incoming caller ID. As it is intended, it shows both who the call is for (in case you have multiple numbers configured on your phone) and who it's from, after a line break. Neither SNOM phones nor linphone, ekiga, etc handle this perfectly. You may either fix this only on your SNOM phone or on all connected clients:

SNOM phone fix only

  • Log into the web interface of your phone (yes, your phone has a web server) by opening its IP address in a browser. You can find the IP address in the phone'…

How to fix: "unexpected token" error for JSON.parse

When using the json gem, you might run into this error when using JSON.parse:

>> json = 'foo'.to_json
>> JSON.parse(json)
JSON::ParserError: 757: unexpected token at '"foo"'
	from /.../gems/json-1.7.7/lib/json/common.rb:155:in `parse'
	from /.../gems/json-1.7.7/lib/json/common.rb:155:in `parse'
	from (irb):1


The error above happens because the JSON you supplied is invalid.

While to_json does work correctly, the result itself is not JSON that can be parsed back, as that s…

Controller specs do not persist the Rails session across requests of the same spec

In specs, the session never persists but is always a new object for each request. Data put into the session in a previous request is lost. Here is how to circumvent that.

What's going on?

You are making ActionController::TestRequests in your specs, and their #initialize method does this:

self.session =

This means that each time you say something like "get :index", the session in your controller will just be a new one, and you won't see …

Markdown-like emphasizing for text fields

Say you want to allow users to emphasize some string, but the whole markdown thing would be far too much. The helper method below does a basic replacement of **some text** with <strong>some text</strong>.

Usage: <%=md @question.title %>.

  def custom_markdown(prose)
    markdown =
    markdown << prose.to_str # make sure the prose gets escaped, even if it is an html_safe string
    markdown.gsub(/(\*\*)(.*?)(\*\*)/, '<strong>\2</strong>').html_safe
  alias_method :md, :custom_markdown
3409 cards