Category: Ruby

Rails form_tag

Hi

I know there are a lot of posts about the rails “dangerous” form_tag helper method, but why not another one.

Before saying anything about it, I want to write down the differences between form_tag and form_for (which was the choosen one previously). form_for is provided for updating data models and form_tag is provided for doing the other form operations (search something, change language in a page, etc).

Ok, after this, let’s go to the battle. This was my code

< % form_tag :controller => 'pages', :action => 'index' , :method => 'get' do %>
Tags : < %= text_field_tag :tags, '', :size => 40 %>
< %= submit_tag 'Search' %>
< % end %>

and I didn’t got the html code what I wanted to obtain, after googling for a while, I found this post which got me to the right way to see the light. The reason of this problem was that I was not using the right syntax for form_tag, which is

form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)

so my code became

< % form_tag({:controller => 'pages', :action => 'index'} , {:method => 'get'}) do %>
Tags : < %= text_field_tag :tags, '', :size => 40 %>
< %= submit_tag 'Search' %>
< % end %>

and I was happy again :-D

Hope this silly problem helps not to waste your time (I did it for you ;-) )

M.

Routes in rails

Two days ago, I was solving bugs in my new professional website (not available yet at http://www.miqueloliete.com) when I stared at the screen and noticed how ugly my url was (something/home/home).

In that moment I remembered that I had read something in Agile Development with rails about how to use routes.rb, so I decided to give it a go.

I add to this files these sentences

map.connect 'home/', :controller => 'home', :action => 'home'
map.connect 'about_me/', :controller => 'home', :action => 'about_me'
map.connect 'cv/', :controller => 'home', :action => 'cv'
map.connect 'projects/', :controller => 'home', :action => 'projects'

and ‘home/home’ became ‘home’, ‘home/cv’ became ‘cv’, etc

M.

Ruby Version Manager (RVM)

Hi

I was fighting with my installed different ruby versions and I had heard about rvm, but I didn’t have use it. I decided to do today and I installed it using

gem install rvm

After it I tried installing two different ruby versions but I always obtained the same ruby version (the aptitude installed one). After that and googling a little I noticed (after typing type rvm | head -n1 and doesn’t read “rvm is a function”) that I had forgotten something in my .bashrc file, which was:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

And after that and opened a different konsole I could change my ruby version using ‘rvm use ruby-version’

Cool!!!!!

M.

Git I

After one of my Rails group meetings and all the great things somebody explained me about this distributed revision control system, I decided to give it a go and I started reading the Scott Chacon’s tutorial in order to start using git in one of my personal projects (my own website still in development).

I have to say that, after the three or four things I have done until now with it, git has surprised me a lot. It’s really fast, clear and it covers all my needs in control system tasks. I am svn user at work and I have to say that git is extremely faster than svn in merges (almost in the two I have done using git instead of svn).

Now I have only a repository in my computer but I supose I will have a central repository in the shared server I have with some friends of mine just in case my computer decides to pass away.

This post is named Git I (I don’t know if I will write more Git related posts) just in case :-)

M.

Railscasts

Hi there

These days I’m taking advantage of my daily train time watching railscasts episodes with my lenovo netbook. It’s really awesome all the things you can learn from these screencasts done by Ryan Bates about how to do things using rails.

Thanks a lot Ryan for sharing your knowledge with us.

M.

Override table’s primary key using migrations in rails.

Hi

When a migration is created, rails add a column named id, which is defined as an integer, as table’s primary key column. Sometimes can happen that you don’t want to name your primary key column id using rails. The way you have to write your migration is this one (rails use integer type by default):


def self.up
create_table :airport, :primary_key => :airport_id do |t|
t.string :iso_code, :limit =>3, :null => false
t.string :name, :limit =>150, :null => false
t.integer :city_id, :null => false
end
end

If you have a “many to many” table probably you don’t want to have this id column in your table and you want to define two columns as primary key. This is the way you have to write a migration if you want to define this primary key:


def self.up
create_table :airports_cities, :primary_key => [:airport_id, city_id] do |t|
t.integer :distance, :null => false
end
end

In both examples, columns defined as primary key don’t have to be defined because rails do it for you. Using this, you won’t have an id column in your table and you’ll be able to use whatever you want as primary key (even strings).

Hope this helps (to my fish-like memory or to somebody else’s brain).

M.

Things to remember 2010-01-12 (former Something I always forget)

Howto create the table to store sessions in Rails

rake db:sessions:create

Howto truncate all *.log files in log/ to zero bytes

rake log:clear

M.

Something I always forget

I always forget how to add a source using rubygems. It’s (using ‘http://gems.github.com’ as example) :

gem sources -a http://gems.github.com

That’s all

M.

will_paginate problem

Sometimes, a search has to be coded in an application and if you are using rails, probably you’ll use will_paginate. It is a good piece of software, easy to install and easy to use but, if it’s not used correctly, problems may appear.

I use it using a form_for rails’s tag and four search criteria in it. After coding it, I added will_paginate and after doing my first search, I paginated to the second page…it came empty. After googling a little, I noticed that I was sending info using POST method and will_paginate needs GET method to work properly, so the only thing needed was:

:html => { :method => :get}

and it worked perfectly, passing parameters to the next pagination.

Hope this helps, it’s something silly but sometimes silly things made people go into the dark side of coding ;-)

M.

Problem installing rmagick (using ruby 1.9.1)

Hi dudes

I’m coding my web application and I need to use the simple_captcha gem, which uses rmagick to generate captchas. I’m trying to install it, I already have imagemagick installed in my debian sid, and I found the following problem


ERROR: Error installing rmagick:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.9.1 extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for cc... yes
checking for Magick-config... no
Can't install RMagick 2.12.2. Can't find Magick-config in /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin

Googling a little I found this page which explains what to do, which is ‘apt-get install libmagickcore-dev‘ as root or using sudo.

After this, a new error appears


Building native extensions. This could take a while...
ERROR: Error installing rmagick:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.9.1 extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for cc... yes
checking for Magick-config... yes
checking for ImageMagick version >= 6.3.5... yes
checking for HDRI disabled version of ImageMagick... yes
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no

Can't install RMagick 2.12.2. Can't find MagickWand.h.

And a new action is taken ‘apt-get install libmagickwand-dev‘.

Problems solved and gem installed.

M.