giant robots smashing into other giant robots

We are thoughtbot. We make web & mobile apps.

Tagged:

Comments (View)

Nesting content_tag in Rails 3

Nesting content_tag in Rails 3 is counter-intuitive. This looks like it will work:


content_tag(:ul) do
  collection.map do |item|
    content_tag(:li, item.title)
  end
end

But it doesn’t. You get an empty list: <ul></ul>. What’s the deal? To fix it, use the concat method:


content_tag(:ul) do
  collection.map do |item|
    concat(content_tag(:li, item.title))
  end
end

Tagged:

Comments (View)

The More You Know: custom time descriptions

Fred asked if we could shorten the “about 1 month ago” text in an information-rich table that was squeezing the created_at data.

wrapping onto multiple lines

I’m not familiar with the i18n API beyond supporting internationalization in Clearance. Therefore, it took me mildly by surprise when I learned that you can override distance_in_words with the i18n API (and, by extension, time_ago_in_words).

Speaks English real good

i18n is the right tool to solve a problem like this even in an English-only application. A quick change to config/locales/en.yml, however, and we’re done:

en:
  datetime:
    distance_in_words:
      less_than_x_seconds:
        other: '1 minute'
      half_a_minute: '1 minute'
      less_than_x_minutes:
        one: '1 minute'
      x_minutes:
        one: '1 minute'
        other: '{{count}} minutes'
      about_x_hours:
        one: '1 hour'
        other: '{{count}} hours'
      about_x_months:
        one: '1 month'
        other: '{{count}} months'
      about_x_years:
        one: '1 year'
        other: '{{count}} years'
      over_x_years:
        one: 'over 1 year'
        other: 'over {{count}} years'

The result:

fits on one line

You can see other options in svenfuchs/i18n.