martedì 14 dicembre 2010

How to send a dinamically generated file in a Rails 3 application

Sometimes can be useful to send a file to the user instead of rendering a page. When you have a fixed location for the file you simply achieve this with a link to the file inside the view, but there may be some cases in wich you have to dinamically generate the file to send. To do this you have to add a new custom action to the controller in wich you want to add the download.
In this example I suppose to have a simple blog application where the user can download the content of a post in text format. So, in the posts controller we add the following action:
class PostsController < ApplicationController

...

def download
  @post = Post.find(params[:id])
  send_data @post.body, :filename => @post.title
end

...

end
After this we have to add a new line into our config/routes.rb file to make this action accessible with a get request:
...
resources :posts do
  member do
    get 'download'
  end
end
...
Now we can add a simple link in our view:
<%= link_to 'Download', download_post_path(@post), :title => 'Download post' %>
Here we suppose to have the instance variable @post created somewhere (i.e. in the show action).
Now we can display our page and download the post as a text file.
Hope this helps, have fun with Ruby on Rails!

mercoledì 17 novembre 2010

PUF: a wget-like url fetcher

PUF stands for parallel url fetcher is a beautyful download tool for UNIX-like systems. It is similar to GNU wget, but with puf you have the ability to download many files in parallel. To install it on Debian-based linux (like Ubuntu) simply type to the command line:
sudo apt-get install puf
The usage is very simple. For example, to download many files in parallel, you should type:
puf file1 file2 file3
and so on...
More info: http://sourceforge.net/projects/puf/ or simply by typing from the terminal:
man puf

Enjoy!

giovedì 11 novembre 2010

Case insensitive uniqueness validation in rails 3

The default uniqueness validation (using sexy validations) in Rails 3 is case sensitive. This means that if you wrote the following class, with the following validation:
Class User < ActiveRecord::Base

  validates :username, :presence => true, :uniqueness => true

end
these usernames will pass the validation and will be stored in the database:
foo, Foo, fOo, fOO, ...
To avoid this problem you should make a change to the previous model in order to make the uniqueness validation case insensitive. To acheive this you have to pass an hash of parameters to the uniqueness validator. The new code may look like this:
Class User < ActiveRecord::Base

  validates :username, :presence => true, :uniqueness => {:case_sensitive => false}

end
Hope this helps :)

giovedì 28 ottobre 2010

Install Ruby Version Manager (RVM) on Ubuntu Lucid/Maverick

RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems. In order to install RVM you need a few packages:
sudo apt-get install build-essential curl zlib1g-dev
libreadline5-dev libssl-dev libxml2-dev
Now you need to install a temporary version of ruby and rubygems. This version will be used to install the rvm gem.
sudo apt-get install ruby1.8 rubygems1.8
After the installation is completed you can install the rvm gem with the command:
sudo gem install rvm
To complete the installation now run:
/var/lib/gems/1.8/bin/rvm-install
As the last step you have to add the following line to the file .bashrc in your home. So edit it with your favourite editor or create it if it doesn't exist.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.
To verify the installation, type rvm -v and the output should be the following:
$ rvm -v
rvm 1.0.15 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]
Now to install ruby inside rvm use the simple command:
$ rvm install 1.9.2
"1.9.2" is the version of the ruby interpreter that you want to install. RVM automatically downloads the sources from the internet, compiles and install them. To set 1.9.2 as the default version use the command:
rvm --default 1.9.2
Enjoy with ruby!

sabato 23 ottobre 2010

Installing Aptana Studio 3.0 on Ubuntu Lucid/Maverick

Aptana Studio is an open source integrated development environment (IDE) for building Ajax web applications. It includes support for JavaScript, HTML, DOM, and CSS with code-completion, outlining, JavaScript debugging, error and warning notification and integrated documentation.
If you install the plugin version of the Aptana Studio IDE on an existing installation of eclipse done by the official ubuntu repository, the installation procedure may not work. To solve this problem you must install a few packages through the command line or through Synaptic. The packages you need to complete the installation of the plugin are:
  • eclipse-plugin-cvs
  • eclipse-jdt
  • eclipse-pde
So, the command you should give is:
sudo apt-get install eclipse-plugin-cvs eclipse-jdt eclipse-pde
After this you can continue the installation. When the installer tells you that there may be software from unsafe software sources, answer yes to finish the installation.
At the end you should restart Eclipse and you're done!

lunedì 18 ottobre 2010

L'aggiornamento di rvm fallisce?

RVM è un tool a linea di comando che permette di installare, gestire e lavorare con più versioni di ruby in ambienti separati sia per l'interprete che per le gem. Durante l'aggiornamento di rvm può capitare che appaiano i seguenti messaggi di errore:
$ rvm update
rvm 1.0.1 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]
info: fetching rvm-1.0.14.tar.gz
info: Extracting rvm-1.0.14.tar.gz ...
error: Error running 'gunzip "/home/davide/.rvm/archives/rvm-1.0.14.tar.gz" | tar xf - -C /home/davide/.rvm/src', please check /home/davide/.rvm/log/extract*.log
info: Installing rvm-1.0.14...
error: Error running 'builtin cd /home/davide/.rvm/src/rvm-1.0.14/ ; ./install', please check /home/davide/.rvm/log/install*.log
Il problema si risolve facilmente aggiungendo il parametro "--head" al precedente comando:
rvm update --head
dopodichè l'aggiornamento procederà regolarmente. Al termine della procedura di installazione è necessario dare il comando:
$ rvm reload
per utilizzare la versione di rvm appena installata.
Info su rvm: http://rvm.beginrescueend.com