firstruby

Master ruby with us

Archive for the ‘firstruby’ Category

‘ordinalize’ in Ruby On Rails

Posted by firstruby on 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"

Posted in firstruby, Useful Links | Leave a Comment »

Configuration for Oracle with Ruby On Rails on Windows

Posted by firstruby on June 26, 2009

To configure the Oracle with Ruby on Rails (RoR) applications, we need to do following setting,

Download Oracle 10g from http://download.oracle.com/otn/nt/oracle10g/xe/10201/OracleXEUniv.exe (to download Oracle setup, you need to create an account)

Install following gems,

gem install activerecord-oracle_enhanced-adapter
gem install ruby-oci8

Then modify the /config/database.yml as following for the current environment,


adapter: oracle_enhanced
host: localhost
username: db_user_name
password: db_password
encoding: utf8

Posted in firstruby, Useful Links | Tagged: , , | Leave a Comment »

ri and rdoc in Ruby On Rails

Posted by firstruby on 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.

Posted in firstruby, Forum, Useful Links | Tagged: , , , | Leave a Comment »

Git – create git branch

Posted by firstruby on November 2, 2008

Create new branch “demo” as,

1) git branch demo

This will create local branch “demo”

2) git checkout demo

This will switched to that branch

3) make some changes in that code, then,

git add <files>
git commit -m "<your message>"

4) git push origin demo

This will create new remote branch “demo” on github.

5) If you want someone to checkout the code from “demo” branch, then do following,

git clone <git-path>

then,

git checkout --track -b demo origin/demo

Now, you are in “demo” branch.

Posted in Blogroll, firstruby, Useful Links | Tagged: , , | 1 Comment »

select with :onchange in Ruby on Rails

Posted by firstruby on November 2, 2008

In Ruby on Rails, We can use ‘select’ with :onchange as following code block,

= f.select :my_attribute, @my_values.collect{|a| [a.name, a.id]}, {}, { :onChange => 'remote_function(blabla....)' }

Posted in firstruby | Tagged: , , , | Leave a Comment »

GIT with branches

Posted by firstruby on October 31, 2008

In SVN,  we can create standard repository structure like,
– trunk       – current code
– branches – versions
– tags
such as we can store the code in different branches in GIT,
git push origin demo1.0
where demo1.0 – name of the branch

Posted in Blogroll, firstruby, Useful Links | Tagged: , | Leave a Comment »

Access Remote MySQL database in RubyonRails application

Posted by firstruby on 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

Posted in firstruby, Useful Links | Tagged: , | 1 Comment »

RESTful architecture

Posted by firstruby on 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

Posted in firstruby, Forum, Useful Links | Tagged: , , | 1 Comment »

DateTime formatting in RubyonRails

Posted by firstruby on 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”

Posted in firstruby, Forum, Useful Links | Tagged: , , , | 2 Comments »

Ruby Gems

Posted by firstruby on 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.

Posted in Blogroll, firstruby, Useful Links | Leave a Comment »