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, year=Date.today.year)
  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”

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: , , ,