undefined method ‘remap’ for class ‘Magick::Image’

Problem
You have just deployed your rails application to dreamhost but you are getting this error from passenger.

Solution
There was a change in the ImageMagic between versions 6.4.3 and 6.4.4 from the affinity function to remap.
If you don’t want, or don’t have time to recompile the ImageMagick and install the rmagick gem, you can comment out the two declarations for the alias functions, located around lines 782 and 1525 in the file rmagick-2.12.2/lib/RMagick.rb:

alias_method :affinity, :remap

Freezing rails to an older version using gems

Problem
You want to deploy to a shared host (dreamhost) that has a later version of rails from the one you have developed your application.
You also have a later version installed in your development pc,and using rake rails:freeze:gems uses the latest one and not the one you want.

Solution
Use the following to freeze the specific version you want, and by using your gems you have installed:
rake rails:freeze:gems VERSION=2.3.2

replacing comma with and in a list with ruby

Problem
You have a list, usually after using join(“,”) in an array that consists of different values ie “one, two, three, four”, but you want to replace the last comma with and so it would be “one, two, three and four”.

Solution
Use reverse initially to reverse the string, then use sub to replace the last comma with the word and reversed (dna), and then finally reverse the string again.

string_with_and=string_with_commas.reverse.sub(/,/, ‘ dna ‘).reversestring_with_no_commas=array.to_sentence(options = {:last_word_connector => ” and “})

NoMethodError: undefined method `timeout=’ for Geokit::Geocoders:Module

Problem
Using the latest version of Geokit gives the error for undefined method ‘timeout’. This could happen if the gem/plugin didn’t modify the config/environment.rb file, and you had to copy the configuration values from an older version.

Solution
The configuration variable has changed in the latest version (1.5.0) from timeout to request_timeout, so change it in your config/environment.rb file as :
GeoKit::Geocoders::request_timeout = 3

Using Flexigrid with MySQL field carriage returns

Problem
You want to use flexigrid with a MySQL fields that contains carriage returns. As the flexigrid uses json it doesn’t work with carriage returns, and displays an empty page, instead of an error page.

Solution
In the page_name.json.erb file in the fields that has carriage returns make sure that you use to_json method as in the example below:

‘<%=fg_escape event.comments.to_json -%>‘,

ferret installation error

Problem
Trying to install ferret with sudo gem install ferret fails with the following error:

fs_store.c:300: error: format not a string literal and no format arguments
make: *** [fs_store.o] Error 1
rake aborted!
Command failed with status (2): [make…]

Solution
As the installation was not possible by using the gem install command, the next step was to download the ferret.tar.gz file extract it and try and install the plugin manually.
It also failed with the same error message.
After some googling for the error message it seems that newer gcc versions fail for non critical errors which in this case is caused by the make options -Wformat -Werror=format-security
So if you replace the:
-Werror=format-security
with
-Wno-error

in the extracted folder/ext/Makefile

and then follow the original instructions as in:

$ rake ext
$ ruby setup.rb config
$ ruby setup.rb setup
# ruby setup.rb install

It should be installed correctly.

Pdf with .doc templates using prawn in Ruby on Rails

Problem
You want to generate pdf documents in your Ruby on Rails app using .doc templates.

Solution
Open the .doc document in OpenOffice and save it as an Open Document Format (.odt).

Open the saved .odt document and export to pdf, making sure you use ‘Losless compression’ in the General Options section.

Use imagemagick’s convert to make the pdf file to an image (setting the density to your requirements – default is 72dpi):

convert -density 150 one.pdf one.png

Copy the image one.png to your rails image directory (public/images).

Use the image as a background to the pdf created by prawnto (install plugin if you don’t have it) with:

templ=”#{RAILS_ROOT}/public/images/one.png”
pdf.image(templ, {:position => :center, :vposition => :top, :scale => 0.215})

Put the database field(s) text on position (you’ll have to play around with the coordinates to get it right), like:

pdf.font “#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf”, :size => 9
pdf.text “#{@model_name.fielda_name}”, :at => [100,530]
pdf.text “#{@model_name.fieldb_name}”, :at => [100,510] …