ArgumentError: invalid byte sequence in US-ASCII

Standard

Installing some gem and you got this error?

I was trying to install Devise:

Using devise (2.1.2) from https://github.com/plataformatec/devise.git (at master)
ArgumentError: invalid byte sequence in US-ASCII
An error occured while installing devise (2.1.2), and Bundler cannot continue.
Make sure that `gem install devise -v ‘2.1.2’` succeeds before bundling.

Solution:

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

How to install Capybara on Linux

Standard

Using the Capybara on Linux, you need to install some dependencies.

You may get some errors like these:

Installing capybara-webkit (0.12.1) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/home/franzejr/.rvm/rubies/ruby-1.9.2-p320/bin/ruby extconf.rb

Gem files will remain installed in /home/franzejr/.rvm/gems/ruby-1.9.2-p320@indica/gems/capybara-webkit-0.12.1 for inspection.
Results logged to /home/franzejr/.rvm/gems/ruby-1.9.2-p320@indica/gems/capybara-webkit-0.12.1/./gem_make.out
An error occured while installing capybara-webkit (0.12.1), and Bundler cannot continue.
Make sure that `gem install capybara-webkit -v ‘0.12.1’` succeeds before bundling.

OR

`ensure_application_exists!’: Xvfb not found on your system (Headless::Exception)

group :test,:development do
     gem 'capybara'
     gem 'capybara-webkit'
end

Firstly you need to use the capybara-webkit and install QT dependencies in your OS.

Ubuntu

sudo apt-get install libqt4-dev libqtwebkit-dev

Ubuntu 10.04 LTS

sudo apt-get install libqt4-dev

Debian Stable

sudo apt-get install libqt4-dev

Fedora

yum install qt-webkit-devel

Fedora 16

yum install qtwebkit-devel

If you are using a recent version of Fedora, the qmake executable is now called qmake-qt4. This will cause the gem installation to fail. So you need to add an environment variable your profile or the system profile.

export QMAKE=/usr/bin/qmake-qt4

Gentoo Linux

emerge x11-libs/qt-webkit

About QT installation you may check here.

You will need to install  xvfb and also use Headless gem. Headless is the Ruby interface for Xvfb. It allows you to create a headless display straight from Ruby code, hiding some low-level action. It can also capture images and video from the virtual framebuffer.

 

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. = )

Why to Create a Seeds File?

Standard

If you are beginner in Ruby on Rails you maybe think If there is some easy way to populate you database. The answer is: Yes! Using Rails you get a lot of productive and keep your focus on the application design. As you know, the Rails uses the design paradigm convention over configuration, which seeks to decrease the number of decision that the developers need to make. With this in mind, if you look at your rails application schema, we can see the file seeds.rb .

At this file, you may put your data which you want to populate your database.

For example:

User.create(:name=> "Harold Marcelo", :age=>30)
User.create(:name => "Jefferson", :age=>28)
User.create(:name=>"Little Boy", :age=>20)

Place.create(:name=> "School")
Place.create(:name=> "Church")

rake db:seed 
#Load the seed data from db/seeds.rb
#OR
rake db:reset
Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.

Now, you can create your administrators, for example. I need to migrate an application and I’ve created a file which contains all the old database,I think this is not the purpose of the seed, but you can use it for things like this too.

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.

Testing Mailers with RSpec

Standard

Important Gems:

1)E-mail Spec

2)Shoulda

3)Shoulda Matchers

Example:

email = UserMailer.create_signup("jojo@yahoo.com", "Jojo Binks")
email.should reply_to("support@myapp.com")

Sending e-mails successfully

it "Should send e-mails successfully" do
        lambda { RegistrationMailer.register(@clients) }.should_not raise_error
 end
it "Should send e-mail to alll admins" do
        admin_emails = Array.new
        @produts.each do |produt|
          produt.company.adminis.each do |admin|
            admins_emails << admin.email
          end
        end
        
        lambda { SalesMailer.send_all(@products).should deliver_to(admins_emails) }
      end