Category Archives: Ruby on Rails

Mongoid localize

If you’re using Mongoid and you need to translate the content of a field in multiple languages (locales) here’re the steps how to do it without additional gems by using “localize” attribute for a field. For example, here’s a simple “Page” model (rails):

class Page
  include Mongoid::Document

  field :title, type: String, localize: true, default: ""
  field :text, type: String, localize: true, default: ""

As you can see, we’ve added “localize: true” to our field definition, which means that our field will be stored with this structure:

  "title" : { "en" : "About us", "it" : "Chi siamo" },
  "text" : { "en" : "English lorem ipsum...", "it" : "Italian lorem ipsum..." }

I’ve set also the default value to an empty string because for some reason when you init a new record it’s initialized with {} ( for ex: {“en” : “{}”} ) instead as a empty string or a null value.Then in your views just print as it was a normal field, for example:


To display the field in a different language just change the locale with I18n.locale = “xx”.
The same way works also update_attributes, just pass it from the form as it was an normal field and call page.update_attributes(params[:page]) for example.

Rails & Amazon SES: send emails with Amazon SES

Amazon AWS launched recently its own email service: Amazon SES: cheap prices and ability to send any type of emails, not just transactional.

In your Gemfile add:

gem "aws-ses", "~> 0.4.1", :require => 'aws/ses'

create an initializer in config/initializers

amazon_creds = YAML::load(open("#{RAILS_ROOT}/config/amazon.yml"))

# extend ActionMailer
ActionMailer::Base.custom_amazon_ses_mailer ={
  :access_key_id => amazon_creds['access_key_id'],
  :secret_access_key => amazon_creds['secret_access_key']

Set delivery mehtod to amazon ses in environment.rb or in a specific environment as needed

ActionMailer::Base.delivery_method = :amazon_ses

If you need to monitor your sending activity there’s a simple sinatra application:

happy mailing!