Sending e-mails with :sendmail in Rails 3

Standard

If you have a Linux Server and you want to send emails in your Rails Application using your own email server please check this. We are using ssmtp.

Put this in your environment.rb:

ActionMailer::Base.delivery_method = :sendmail
        ActionMailer::Base.sendmail_settings = {
          :location => "ssmtp",
          :arguments => "-i"
        }

Common Mistakes

Errno::EPIPE: Broken pipe

Maybe cause you are using the argument “-t” rather than “-i”.

SSMTP is a simple and powerful way to send e-mails, mainly now in your rails application. = )

Factory Girl and the Builder Pattern

Standard

You want to test your code, for example, you will do an Integration Test. At each time We will really create our objects?
Using factories, you will define it just once. With this in mind, you can have focus on test. We want keep focusing on our tests. Factory Girl is widely used and implements the Factory Pattern.

FactoryGirl.define do
  factory :user do
    sequence(:name) {|n| "Name_#{n}"}
    sequence(:email) {|n| "Name_#{n}@email.com"}
    sequence(:password) {|n| "password_#{n}"}
  end
end

# Let's define a sequence that factories can use.  This sequence defines a
# unique e-mail address.  The first address will be "somebody1@example.com",
# and the second will be "somebody2@example.com."
Factory.sequence :email do |n|
  "somebody#{n}@example.com"
end

These factories can be used like so:

@user = FactoryGirl.create(:user)

Factory Girl gem

Like Factory Girl, we have others gems with the same purpose:

  1. Foundry (http://github.com/jeremymcanally/foundry/tree/master)
  2. Machinist (http://github.com/notahat/machinist)
  3. ObjectDaddy (http://tasks.ogtastic.com/projects/show/object-daddy)

With the Factory Pattern we can create several different but related kinds of objects in one step. But if You cannot create a lot of objects in a single step,maybe You will use the Builder Pattern. The Factoy Pattern can be seen as a simplified version of the Builder pattern. The Builder Pattern isn’t used in Ruby On Rails.

Bricks  is a hybrid gem with an Object Builder/Factory implementation.

Installing rmagick (2.13.1) with native extensions – Problem!

Standard

Problem:

Installing rmagick (2.13.1) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/Users/yourUser/.rvm/rubies/ruby-1.9.2-p320/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5… yes
checking for clang… yes
checking for Magick-config… yes
checking for ImageMagick version >= 6.4.9… yes
checking for HDRI disabled version of ImageMagick… yes
checking for stdint.h… *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Solution(Mac OS X Lion):

brew remove imagemagick 
brew install imagemagick --disable-openmp --from-source
bundle install

Possible problems with rails + mysql on OS X Lion

Standard

If you are trying to run your app with Rails and you got some errors when execute the rake db:migrate It is possible that you must to install the GCC. In Lion installation there is no gcc default. You can check GCC here: https://github.com/kennethreitz/osx-gcc-installer

Other possible error is the lack of a  symbolic link:

( Library not loaded: libmysqlclient.18.dylib )

ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

With these changes you may run successful the mysql + rails in your app.

Remembering that is a good practice to reinstall your gem after these changes:

gem uninstall mysql2

bundle install

Problems with Ruby on Mac OS X – Lion

Standard

When I’ve updated to Mac OS X Lion, I’ve found some error running rails application. The problem was  with my ruby installation. I got some sqlite3 errors, then I need to updated my Xcode to 4.3 version and install the Command Line Tools(Xcode->Preferences->Downloads).

After this, some errors appear:

ERROR:  Error installing sqlite3: ERROR: Failed to build gem native extension. 

and

try_do': The complier failed to generate an executable file. (RuntimeError) You have to install development tools first. 

Solution:

Reinstall ruby

rvm reinstall 1.9.2

But you may to get this message:

The provided compiler '/usr/bin/gcc' is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`.

So you can use:

rvm reinstall 1.9.2 --with-gcc=clang