Screencast su Rails e premi per patch sulla documentazione

| |

Ruby è un linguaggio ad alto livello, esplicito e coinciso; con Rails si alza ancora di più e raggiunge un'espressività impressionante.
Rails è in costante crescita e cambiamento, ogni giorno ci sono novità ed è difficile starci dietro.
La documentazione c'é, ma spesso non basta per apprendere e utilizzarlo al meglio, oltre la galassia di, c'é un sito che fornisce ogni 2 o 3 giorni degli screencast gratuiti su come utilizzarlo più efficaciemente.

Questo sito è e per aumentare la diffusione di questo framework mette in palio diversi premi(ipod e Nintendo DS) per chi propone delle patch entro il 27 luglio per aumentare la documentazione di Rails.

Approfitto per consigliarvi 3 screencast che mi sono piaciuti
#57 Fa vedere come gestire i checkbox multipli
#55 Pulire la vista dalla logica
#54 ruby-debug per trovare i bug e provare la correzione a runtime

10 Ruby and Rails related tecnologies for Web2.0 and beyond

| |

1. UJS
Unostrubsive Javascript, write in web1.0 and render in Web2.0, Web3.0,.. This is an attempt to add behaviors to the MVC pattern. The power of the standards but also integrated effects.

2. Globalize
A smart way to add internationalization and globalization to your sites

Provide datas corresponding to the request easly

4. RSpec
Add readable and easy to mantain tests to your code

5. Rjs
Write the code in ruby also for the client-side

6. Capistrano
Develop locally and put the working code easly in production

7. Webserver indipendent
Apache, lighttpd, mongrel,.. Choose the webserver with the performance, features you need

8. Database indipendent
SQlite, Mysql, Postgresql, Firebird,.. You can choose the backend technology you prefere (as above)

9. Free Text editors / IDE with plugins, snippets, syntax highlight, autocompletion,..
Gedit textmate-like, Eclipse RDT/Aptana, Ride-me

10. Community
Documentation, Screencasts, Forums(1,2,3)

How to map Glade/Gtk widgets to instance variable without repeating yourself

| |

Glade is nice tool: it creates the xml of the inteface you see, but when you have to use this gui you have to map every widget created with an instance variable.
This is easy to mantain when you have few widgets, but when you have a lot textbox / entry you will have to map a lot of stuff.
Here's a Ruby version of a runtime code generation using the eval method

Download first the
# gui.rb

require 'libglade2'

class Gui

 def initialize
  @glade ="") {|handler| method(handler)}
  @glade.widget_names.each do |name|
       instance_variable_set("@#{name}".intern, @glade[name])
       if @glade[name].class == Gtk::Entry
        # automatic version
       eval <<-END
        @#{name}.signal_connect('changed') do
          @window1.title= @#{name}.text
        @#{name}.text= "My name is @#{name}"
  # hand version

Yo will be able to access to any entry widget with @entry and set a common signal to all of them without doing it for each widget.

Howto manage an svn+edgerails+capistrano environment on Gnome/Linux

gedit rails svn scripts
 I found on internet a lot of informations about how to install something-in-rails but I didn't find a real world usage of it.
What I've written is an overview about how to be productive with rails in a quite common use case. This "ecosystem" is mainly for rails projects, but can be used also for other kind on applications.  

Install svn

Subversion is a very common version control system; sometimes it's installed in your web hosting space, but if you decide to set up on you PC you are more indipendent.
 sudo apt-get install subversion subversion-helper-scripts subversion-tools 
You have to create the directory for the svn projects

# '/svn-stuff' is where you put your config and svn internal stuff
sudo mkdir -p /svn-stuff
sudo svnadmin create /svn-stuff
-- sudo gedit /svn-stuff/conf/svnserve.conf
anon-access = read
auth-access = write
password-db = passwd # relative path to the users file

-- sudo gedit /svn-stuff/conf/passwd
username = password


Start svn server (called svnserve) with:

svnserve -d -r /svn-stuff
# it means 'daemon' and svn projects path

There's not a '/etc/init.d/svnserve start' script but you can start automatically svn server with xinetd

For more informations see Ubuntu forum and if you need the http interface you can install websvn


+ Everything should go as expected, if you get something like "svnserve.conf:28: Option expected" probably you have to delete the space before a parameter like anon-access
+ You can start svnserve with -i, but It didn't work for me

Install edgerails environment

It's very easy to do 'gem install rails' but if you want lastest stable release or your web hosting doesn't update rails frequently this is the best thing to do.
We will keep latest rails in a directory called edgerails and all the apps we'll create will point the first rails path MYRAILSAPP/vendor/rails -> to edgerails/ So edgerails will be shared.

# '/projects' is the path to the dir where you store your apps
cd /projects
svn co edgerails/
#This alias is to have edgerails as default rails
alias rails="ruby  /projects/edgerails/railties/bin/rails"
rails eapp
cd eapp/vendor/
ln -s ../../edgerails/ rails
cd ..
script/server # http://localhost:3000 and test rails version
# => Booting lighttpd (use 'script/server webrick' to force WEBrick)

cd ..

# It's the first import of the directory, and 'trunk' is the current release
svn import eapp svn://localhost/eapp/trunk -m "Import eapp"

# just to be secure that everything will go well anyway :)
mv eapp eappbak

# This will sinchronize the content with the current svn version

svn co svn://localhost/eapp/trunk eapp # with -r <number> you specify the release

#Test svn
cd eapp
echo "test" >> README
svn diff # differences with svn version
svn ci -m "added test ooh" # commit the changes to svn
svn up -r <number before> #rollback
# svn add file-or-dir # to add a file to svn

Common tasks

# -x automatically generates the svn code
script/plugin install -x acts_as_taggable
# -x automatically generates the svn code
script/generate model -c mymodel

Other stuffs

Capistrano permits to create tasks to manage your rails app
# Usually the web hosting already did this for you
sudo gem install capistrano --include-dependencies

cap --apply-to eapp
svn add config/deploy.rb
svn add lib/tasks/capistrano.rake
svn ci -m "added capistrano"
Install features for Gnome environment

nautilus svn scripts
to control common svn commands on a GUI!
sudo apt-get install nautilus-script-collection-svn
meld is a nicer way to see 'svn diff' with colors
edit the nautilus script 'diff' and put:
meld $@

Gedit rails, some basic autocompletion snippet for rails


  • If you have enought RAM and CPU even Eclipse + Radrails + Subversive offers a nice environment.

Reverse URL translation with Globalize - Rails

Globalize is one of possible way(see the comparison) to translate a Rails app, you are forced to use a database to do this but It's really a powerful method to localize  an application because this plugin adds a method .t to every String.

Unfortunatly the method .t is just for English-to-otherLanguage translation so you have to hack it a bit to have reverse translation.

Here's the method I used to translate the URL:

  • Name of the controller statically translated in routes.rb
  • Translation of each chunk after in the controller

So I'll be able to manage, at the same way, url like:

# config/environment.rb
# [...]

include Globalize
Locale.set_base_language 'en'

# enables reverse translation and redefine String#t
class String

alias :old_t :t

def t
if self.old_t == self.to_s
q = Translation.find_by_sql("SELECT tr_key FROM globalize_translations g where text='#{self.to_s}'")

if q.empty? # there's no translation
return self
return q[0].tr_key


Restart /script/server 

# config/routes.rb
# [...]

  map.connect 'ristorante/*url' ,
:controller => 'restaurant', :action=>'translate', :language => 'it'

  map.connect 'restaurant/*url' ,
:controller => 'restaurant', :action=>'translate', :language => 'en'
# app/controllers/restaurant_controller.rb
# [...]

def translate
if params[:url].nil? or params[:url].empty?
@url = []


case url_join

# url empty
when ''
render :text => 'main'

# restaurant/ + recipes/pizza.html
when /^\w+\/+\w+\.html$/ then

# try to search 'pizza'.t in the db, without '.html'
@recipe= Recipe.find(:first, :conditions=> ["name = ?",@url[1].split('.')[0].t])

if @url[0] == 'recipes'.t
render :partial => 'view_recipe'
# if the same regexp match to some other subaction
elsif @url[0] == 'ingredients'.t
render :view_ingredients => 'view_ingredients'
render :text => 'Error 404' --> '+url_join


Probably this solution isn't very scalable, because it does to many queries, so if you adopt an url like instead of, you will save the reverse query, because the id is already present.

Syndicate content