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"

Backup of SVN repository,

svnadmin dump myrepos > dumpfile

Once you give this command, it will create the dumpfile (backup) of the SVN repo with all the LOGs.

Load the SVN dump file on server,

svnadmin load newrepos < dumpfile

Once you give this command, all the data from the dumpfile will be loaded/extracted with all SVN LOGs.

Reference URL – http://chestofbooks.com/computers/revision-control/subversion-svn/Migrating-Repository-Data-Elsewhere-Reposadmin-Maint-Migrate.html

How to create rails application for specific rails version ?
If you have multiple Rails versions installed on your application and if you want to create Rails application,

rails demo

then in this case, it will create Rails application with latest version installed on our machine.

If you want to create Rails application for specific rails versions, use following command

rails _2.1.0_ demo

demo – rails application name with version 2.1.0

If you want to display the list ( of users ) with different css-class for alternate record, there is no need to check the odd-even records,

For this, Rails have awesome feature and that is – ‘cycle’

We can use this as,

say we have 2 css classes-blank and white. If want to apply ‘black’ and ‘white’ class for alternate user record, then we have to write the code as,

<% @users.each do |user| %>
<li class="<%= cycle('black', 'white') -%>">
<%= user.name %></li>
<% end %>

Cool feature :)

Previously I was adding most of the code in one controller…say if there is admin module, and there are different sections in it, then it’s better to add the code in separate controllers – sub-controllers.

Here is the way to create sub-controllers,

ruby script/generate controller admin/users

It will create users_controller.rb in /controllers/admin folder (sub-folder for controller)

Your user controller will look like,

class Admin::UsersController < ApplicationController
end

And to inherit the properties of the parent controller (AdminController), you can change parent controller as AdminController instead ApplicationController

class Admin::UsersController < AdminController
end

Hope this helps.

This post is password protected. To view it please enter your password below:


To copy a file/directory in working directory or in otherdirectory,

svn move SOURCE DESTINATION

if you want to move ‘app’ folder from working directory into /trunk,

svn move app /trunk/app

it will move ‘app’ folder in ‘/trunk/app’ folder.

Similarly to copy the files,
——————————————–
svn copy SOURCE DESTINATION

if you want to copy ‘app’ folder from working directory into /trunk,

svn copy app /trunk/app

it will copy ‘app’ folder in ‘/trunk/app’ folder.

Android ?

November 22, 2008

Android ………Will post in details soon..

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.

We can pass the multiple parameters with remote_function or link_to_remote…

<%= link_to_remote "View Data",
:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " %>

or

<%= remote_function(:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " ) %>