Different layout for the same controller

In some projects can happen that you have to use different graphic templates for different methods of the same controller. At first sight one would implement a solution that provides an explicit call to the desired layout within each action.Something like this:123456789101112131415161718192021class PostController < ApplicationController def index [...] :layout => 'standard' end def show [...] :layout => 'standa...

Read more

How to install MySQL and Rails on Osx Snow Leopard

Hello everyone. These days I've bought a MacBook Pro with Osx Snow Leopard 64-bit pre installed. Having to develop mainly in Rails and Django on MySQL and PostgreSQL, I've found some difficulties to install and properly configure all packages. The main problems were due to the presence of 32bit libraries and other at 64bit. For example, Python or Ruby (already installed by default) were com...

Read more

Rails Best Practices 6: Filters and helpers file

With the today’s post I shall conclude my series on Ruby On Rails Best Practices . This is not because the required topics are completed, but rather because after the release of Rails 3.0, some constructs have been changed and should be reviewed. Probably in future we will return to talk about best practices, but starting from the new features introduced in the current Rails version. ...

Read more

Rails Best Practices 5: Optimize Migration

Migrations, in my opinion, are one of the best things in Rails since these allow the creation and populating the database using ruby code without having to worry about which type of db run below. That said, even writing the migration is better to follow some best practices. 1. DB Index The first practice I strongly recommend is to define indices for the external keys and fo...

Read more

Design Patterns in Ruby: Chain of Responsibility

Today's post discusses the first of the behavioral pattern shown by the GoF, the chain of responsibility. This pattern expects a series of commands to be executed and a set of objects capable to handle them. Each of these "handler" objects can send the command to the next handler in the chain if it is not able to carry it out. A mechanism also exists for adding new handler objects to the end of this chain. To show this pattern, we take as example a rea...

Read more

Design Patterns in Ruby: Adapter

This second post of the series leaves for a moment the creational patterns and speaks about one of the most important structural pattern: the Adapter. The purpose of an adapter is "to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces." Suppose therefor...

Read more

Rails Best Practices 2: Move logic to model

Today we continue our analysis of the Rails Best Practices. In the previous post we saw Named Scope, Model Association and Following the same direction, in today’s post we’ll examine the use of Callback Model and Virtual Attribute.1. Virtual AttributeSuppose we have a customers list table defined as follows123456create_table "clients", :force => true ...

Read more

Design Patterns in Ruby: Abstract Factory

An abstract Factory provides a common interface for creating families of related objects together. The client object does not bother to build objects directly, but it calls the methods provided by this common interface. Below is showed one possible implementation of an abstract Factory and its concrete Factories that implement it. Suppose we have two c...

Read more