The Latest

Spellchecker in Ruby on Rails

On June 17, 2011, in rails, by gustavo.cardenas
0

There is a good way to implement a spellchecker in Ruby on Rails across the gem raspell that uses the dictionary of aspell to work, and here I let you a quick way to implement it.

First we install aspell (http://www.aspell.net):

Mac:
sudo port install aspell aspell-dict-en

Ubuntu:
sudo apt-get install aspell libaspell-dev aspell-en

After that we install the gem.

Mac:
sudo gem install raspell –with-opt-dir=/opt/local

Ubuntu:
sudo gem install raspell

With this you have now the possibility to spellcheck words in your application.

And that’s it, you could use it in a simple way like this:

require ‘rubygems’
require ‘raspell’

speller = Aspell.new(‘en-US’)
speller.suggestion_mode = Aspell::NORMAL

word = “haert”
if !speller.check(word)
#word is wrong
speller.suggest(word).first
end

You could have more information in the github repository: https://github.com/jimneath/raspell.

 

Attachment_fu and amazon s3 (with Windows fix!)

On December 3, 2009, in rails, by lsanvitale
0

Problem: We have a model(my_image_model), that stores images to our file_system but now we want to start storing images to amazon S3.

Don’t worry, in eight simple steps we can acomplish that. Let’s start!

Solution:

  1. Download a client to manage amazon’s S3 interface ( I use firefox plugin, S3 organizer)
  2. Access the S3 account and create a bucket (eg: my_bucket)
  3. Inside my_bucket, create a folder uploads
  4. In our environment, install the gem aws-s3 (http://amazon.rubyforge.org/). From the console: gem i aws-s3
  5. Configure S3 credentials. The file amazon_s3.yml must exists and be in the folder /config of our project
  6. development:
    bucket_name: dev_my_bucket
    access_key_id:my_account_access_key_id
    secret_access_key:   my_account_secret_access_key

    test:
    bucket_name: test_my_bucket
    access_key_id:my_account_access_key_id
    secret_access_key:   my_account_secret_access_key

    production:
    bucket_name: prod_my_bucket
    access_key_id:my_account_access_key_id
    secret_access_key:   my_account_secret_access_key

  7. Modify the model my_image_model.
  8. has_attachment :storage => :s3,:path_prefix => “/uploads”,:content_type => [  "image/jpg",  "image/jpeg",
    " image/pjpeg",  "image/gif",  "image/png"], :max_size => 2.megabyte,
    :thumbnails => { :home => ’969×526!’, :page=>’969×480!’, :slideshow=>’534×478!’,
    :thumb=>’52×28!’ }

    validates_as_attachment

  9. If you are in a windows environment you must add the following fix (http://nhw.pl/wp/2008/02/05/why-are-you-doomed-being-ruby-developer-on-windows-platform). Adding this fix is really simple, you have to modify the file ../vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/s3_backend.rb , the method save_to_storage:
  10. def save_to_storage

    if save_attachment?
    #   S3Object.store( full_filename, (temp_path ? File.open(temp_path) : temp_data),
    #   bucket_name,  :content_type => content_type,
    #   :access => attachment_options[:s3_access]   )

    #   FIXME: ugly hack to get it running on windows somehow when You give file handle it won’t read it
    if RUBY_PLATFORM =~ /(:?mswin|mingw)/
    file_content = “”
    if temp_path
    file_content  = File.open(temp_path, “rb”).read
    else
    file_content = temp_data
    end
    S3Object.store(full_filename,file_content,bucket_name,:content_type => content_type,
    :access => attachment_options[:s3_access])
    else
    S3Object.store(full_filename,(temp_path ? File.open(temp_path) : temp_data),
    bucket_name,   :content_type => content_type,   :access => attachment_options[:s3_access])
    end
    end
    @old_filename = nil
    true

    end

  11. You are done! You can start uploading files to your S3 bucket!