‘ordinalize’ in Ruby On Rails
July 21, 2009
Print numbers as Cardinals in Ruby On Rails (like 1st, 2nd, 3rd, 4th and so on….)
Rails has inbuilt function – ordinalize
(1).ordinalize => "1st"
(13).ordinalize => "13th"
(22).ordinalize => "22nd"
(100).ordinalize => "100th"
Also we can write the helper as following,
def number_to_ordinal(num)
num = num.to_i
if (10...20)===num
"#{num}th"
else
g = %w{ th st nd rd th th th th th th }
a = num.to_s
c=a[-1..-1].to_i
a + g[c]
end
end
number_to_ordinal(23) => "23rd"
number_to_ordinal(1) => "1st"
ri and rdoc in Ruby On Rails
November 16, 2008
To install ri and rdoc of any gem,
gem install gemname
This command installs ri and rdoc with installation of gem.
To skip the installation of ri and rdoc of gem,
gem install gemname –no-ri –no-rdoc
This command will skip the installation of ri and rdoc when installing gem.
GIT with branches
October 31, 2008
Access Remote MySQL database in RubyonRails application
October 23, 2008
To give the remote access to database, on server, we have to give the following command,
mysql> CREATE DATABASE foo_development;
mysql> GRANT ALL ON foo_development.* TO root@'192.168.1.25' IDENTIFIED BY 'PASSWORD';
where 192.168.1.25 – IP Address of the client machine,
Now modify ../config/database.yml file in our RoR application as follows,
development:
adapter: mysql
database: foo_development
username: root
password: PASSWORD
host: 192.168.1.1
where 192.168.1.1 – IP Address of server
RESTful architecture
May 29, 2008
RESTful interface means clean URLs, less code, CRUD interface.
CRUD means Create-READ-UPDATE-DESTROY.
You might heard about HTTP verbs, GET, POST. In REST, they add 2 new verbs, i.e, PUT, DELETE.
There are 7 default actions, those are – index, show, new, create, edit, update, destroy
GET is used when you retrieve data from database. POST is used when you create new record in database. PUT is used when you are updating any existing record in database, and DELETE is used when you are destroying any record in database. Following table may clear the concept.
Action VERB
index GET
show GET
new GET
create POST
edit GET
update PUT
destroy DELETE
DateTime formatting in RubyonRails
May 29, 2008
To display datetimes in different formats in Ruby On Rails application,
following may help,
Format meaning:
%a – The abbreviated weekday name (“Sun”)
%A – The full weekday name (“Sunday”)
%b – The abbreviated month name (“Jan”)
%B – The full month name (“January”)
%c – The preferred local date and time representation
%d – Day of the month (01..31)
%H – Hour of the day, 24-hour clock (00..23)
%I – Hour of the day, 12-hour clock (01..12)
%j – Day of the year (001..366)
%m – Month of the year (01..12)
%M – Minute of the hour (00..59)
%p – Meridian indicator (“AM” or “PM”)
%S – Second of the minute (00..60)
%U – Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W – Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
%w – Day of the week (Sunday is 0, 0..6)
%x – Preferred representation for the date alone, no time
%X – Preferred representation for the time alone, no date
%y – Year without a century (00..99)
%Y – Year with century
%Z – Time zone name
%% – Literal “%” character
For example,
t = Time.now
t.strftime(“Printed on %m/%d/%Y”) #=> “Printed on 04/09/2003″
t.strftime(“at %I:%M%p”) #=> “at 08:56AM”
Ruby Gems
February 4, 2007
Using Ruby Gems
You may want to add functionality to your application using Ruby Gems. A gem is a packaged Ruby application or library. A great introduction to Ruby Gems can be found here. Basically, Ruby Gems keep you from having to reinvent the wheel, by providing you with modular pieces of code that can be accessed from within your application’s code.
By default, some gems will come pre-installed with your hosting plan. Generally these are Rails-relevant gems (ActiveRecord, ActionMailer, etc.). To see a list of installed gems at any time, type “gem list –local” from an SSH window.
To install new gems, issue the command “gem install <gem-name>” (where <gem-name> is the name of the gem you wish to install). This command will first search your local machine for the specified gem, and if not found, will attempt to automatically download the gem from RubyForge, an online gem repository. Check RubyForge for a complete listing of available gems or check the link here for a more condensed listing. More installation options can be found by typing “gem install –help”.
By default, gems will be installed to ‘/usr/lib/ruby/gems/1.8/gems/’.
Freezing Ruby Gems
Whenever a new gem version appears on the host, your Rails application will attempt to automatically upgrade to that version. This is convenient when in development, but after moving to production, it can cause things to break unexpectedly. For obvious reasons, you don’t necessarily want your application being updated after it has been published. To prevent gems from updating, change directories to your application root, and issue the command “rake freeze_gems”. This copies the gems from /usr/lib/ruby/gems/1.8/gems/ (the default location) to <your-app-name>/vendor/ and keeps them from being automatically updated.There is one caveat with the “rake freeze_gems” command. It will only freeze gems that are Rails-relevant. If running this command doesn’t freeze the gem(s) you want it to, (i.e. they are not found in <app-name>/vendor/) you will need to use the solution found here.
This section is intended only to give a brief introduction to concept of freezing gems. If this is something you are interested in doing, it is highly recommended that you read up on the documentation at http://www.rubygems.org for more information.
Mongrel_server
January 5, 2007
Getting Started
The easiest way to get started with Mongrel is to install it via RubyGems and then run a Ruby on Rails application. You can do this easily:
gem install win32-service (pick the most recent one)
gem install mongrel (pick the win32 pre-built)
Win32 Support
Win32 is fully supported by Mongrel with services and CPU affinity support.
The main thing with Win32 support is that there is no fork API for Ruby, so you have to use the services features to get persistent servers running. You can get this services support by doing:
gem install mongrel_service
And then just run mongrel_rails to see what services:: commands are available.
Running The Service
After you do the gem install, find a Rails application you want to run and do:
$ mongrel_rails service::install -N myapp \\
-c c:mypathtomyapp -p 4000 -e production
$ mongrel_rails service::start -N myapp
Now hit the port and poof, works (or should).
The application will stop if you use:
$ mongrel_rails service::stop -N myapp
NOTE: Stop reports an exception but does stop the service.
Now the really great thing is that you can just do all this from the Services control panel like your Rails application is a regular Windows service.
Even works in development mode, which is pretty nice.
Making you Service autostart with Windows
By default, the new Mongrel service get installed to be run manually, using Mongrel’s commands or the Service Manager. You could tweak it to start automatically when Windows start, just use the Service Control commandline tool:
$ sc config myapp start=auto
Also, you can configure the services that are neede to make it work, like database support:
$ sc config myapp start=auto dependency=MySql
CPU Affinity
Mongrel’s win32 support actually is able to set the CPU affinity of a running Mongrel service. This is pretty neat since it means if you’re running a fancy SMP machine or a dual core that pretends to be SMP, then you can force Mongrel onto one of them and get a nice little boost.
It’s pretty easy to use, just pass the -u or—cpu option to the install command and give a CPU of 1-X. That means if you have 4 CPUs and you want Mongrel on #4 then do:
$ mongrel_rails service::install -N myapp
-c c:mypathtomyapp -p 4000 -e production -u 4
Pretty much the same command, just one more option and you’re done.
Welcome
December 13, 2006
Welcome to learn Ruby,
Now you can download ‘Radrails (version 0.7.2)’ editor for developing Ruby On Rails application for Windows, Mac OSX and Linux at