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.

sinatra and i18n gem

To make work i18n gem and sinatra in your add:

# include the gem, otherwise add it to your bundle file if you manage gems with bundler.
require 'i18n'

# assuming that you have your locale files (.yml) in ./config/locales directory
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'config', 'locales', '*.yml').to_s]

Then somewhere in you app add the t() helper:

helpers do
  def t(*args)

Happy internationalizing!

Ruby: uninitialized constant Syck::Syck

I was trying to install riak-client gem using bundler and ruby 1.9.2 and I got:

Fetching source index for
/Users/dumitru/.rvm/rubies/ruby-1.9.2p180/lib/ruby/site_ruby/1.9.1/rubygems/specification.rb:289:in `load': uninitialized constant Syck::Syck (NameError)
 from /Users/dumitru/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/specification.rb:289:in `_load'

To solve this just run:

gem update --system

Using git with dropbox

1) init the “remote” repository:

cd ~/Dropbox/code && mkdir new_project && cd new_project && git init --bare

replace code and new_project with whatever you like.

2) if you didn’t initialized your local repository, run the following on your local project directory (optional):

git init
git add .
git commit -am "Initial commit"

3) then add the remote repo to your local project:

git remote add origin ~/Dropbox/code/new_project

replace origin with any repository name you like

4) push/pull changes like working with a “normal” git repository:

git push origin master
git pull origin master

Happy coding!

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!