Blur links with jQuery

While surfing the Web, you probably noticed the dotted outline, that appears when you click on the link. If your link doesn’t lead to another page, but instead triggers some event in the same page, outline stays there and looks ugly.

For modern browsers you can remove it with simple css:

a:focus, a:active {

Unfortunately, this won’t do for IE 6 and earlier.

With jQuery it’s easy to get rid of the outline in every browser that has JavaScript enabled:

$("a").click(function() {

This will remove outline from the link once he is clicked. You will see the outline when clicking, but it will not stay there.

If you don’t want to se the outline at all replace click event with focus event:

$("a").focus(function() {

To find more about jQuery Events visit

Disabeling sessions in Rails

The simplest way to disable sessions in Rails is to use session :off (see ActionController::SessionManagement::ClassMethods).

session :off

To disable session suport only for a specific controller add session :off to that controller

class MyController < ApplicationController
    session :off

Written like that, sessions are disabled for all actions on this controller.

Like filters, you can specify :only and :except clauses to restrict subset. The following code will disable session for first_action and third_action, but not for second_action.

class MyController < ApplicationController
  session :off, :only %w(first_action third_action)
  def first_action

  def second_action

  def third_action

Continue reading “Disabeling sessions in Rails”

Determining the number of days in a month

Here is a simple method that returns number of days for wanted month and year. If year is ommited current year is selected.

def month_days(month,
  mdays = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  mdays[2] = 29 if Date.leap?(year)mdays[month]

Simple examples:

month_days(1)       => 31
month_days(2, 2007) => 28
month_days(2, 2008) => 29

Now, let’s assume we have a date from a database, and we want to find number of days in a month from that date.

month_days(date.month, date.year)

But with two more lines of code

Continue reading “Determining the number of days in a month”

Ruby Inject

You probably frequently have to iterate through a list and accumulate a result that changes as you iterate. Let’s take the following array:

nums = [1, 3, 5, 7]

If you want to find a sum of all the items in the array, you could write something like this:

sum = 0
nums.each { |n| sum += n }

This works fine, but it doesn’t look much elegant. Luckly, for us that wants to be elegant there is a inject method.
(See: Enumerable::inject)

sum = nums.inject(0) { |x,n| x+n }

This code does the same thing, but also looks cool. Inject acomplish this by using accumulator or accumulated value (x). At each step accumulated value is set to the value returned by the block. Here, we set accumulator initial value to 0, and at each step current value from array (n) is added to accumulated value.

If initial value is omitted, the first item is used as the accumulator initial value and is then omitted from iteration.

sum = nums.inject { |x,n| x+n }

Is same as

sum = nums[0]
nums[1..-1].each { |n| sum += n }

Another example.
We have array with five names:

names = ["michael", "ron", "scottie", "dennis", "toni"]

The following code:

string = names.inject("") { |x,n| x << "#{n} " }

Will output:

=> "michael ron scottie dennis toni "

You can get the same output with:

string = ""
names.each { |n| string << "#{n} "}

Now, let’s say you won’t to separate them with comma, and capitalize those that have more then four characters.
You can do it like this:

string = ""
names.each do |n|
  name = n.length > 4 ? n.capitalize : n
  n == names.last ? string << name : string << "#{name}, " 

Or like this:

string = names.inject("") do |x,n|
  name = n.length > 4 ? n.capitalize : n
  n == names.last ? x << name : x << "#{name}, "

In both ways we get the same result:

=> "Michael, ron, Scottie, Dennis, toni"

Technorati Tags: , , ,

Quick Start with Rails on OS X

Install Ruby

OS X (10.4) comes with Ruby 1.8.2 already installed.
For a quick start this is OK so let’s install Rails.

Install Rails

Since Rails comes as gem package, first we need to install RubyGems.
Download the latest package from RubyForge and unzip it.
Now go to the directory where you unpacked the package and run

$ sudo ruby setup.rb

When asked enter your password, and after install finish you can verify that everything went well by typing:

$ gem -v

Output should look something like this, depending on version you installed

=> 0.9.4

Now installing Rails is as simple as

$ sudo gem install rails --include-dependencies

To verify installation, type:

$ rails -v
=> Rails 1.2.3

Install MySQL

You can skip this step if you don’t want to use database but since Rails is a framework for developing database-backed web applications, you’ll probably want to use one.

  1. Download the MySQL package for OS X for your platform
  2. Double-click the drive image to mount it
  3. Locate the MySQL installer (something like mysql-5.0.45-osx10.4-i686.pkg) and run it, authenticating as needed
  4. Double-click MySQLStartupItem.pkg
  5. Double-click MySQL.prefPane and install it

Once the install is complete, start the MySQL server using the control panel. ( System Preferences -> Other -> MySQL ) and check Automatically Start MySQL Server on Startup

Test your installation

To start a new project (application), go to the directory where you plan to keep your applications (i.e. ~/Apps) and execute following commands

$ rails test_app
$ cd test_app
$ ./script/server

This will generate Rails skeleton in ~/Apps/test_app, and start a WEBrick server on port 3000.
Now open your browser and enter http://localhost:3000 in address bar. You should see a page with “Welcome aboard, You’re riding the Rails!” message.

We also need to tell Rails which database to use.
To do this edit database.yml file (~/Apps/test_app/config/database.yml) to look like this

  adapter: mysql
  database: test_app_development
  username: root
  host: localhost

  adapter: mysql
  database: test_app_test
  username: root
  host: localhost

  adapter: mysql
  database: test_app_production
  username: root
  host: localhost

Naturally we also need to create a database

$ mysqladmin -u root create test_app_development

and table in that database

$ mysql -u root

mysql> create table items ( 
         name VARCHAR(100) );

Execute scaffold command

$ script/generate scaffold item

Open http://localhost:3000/items and if you see something like this

Listing items


New item

you have everything needed to get started with Rails development so Enjoy!

Technorati Tags: , , ,