Friday 29 April 2011

Replacing RoR default prototype library with Jquery Via Jquery-Rails Gem

RoR is shipped with Prototype javascript library, it's pretty rich in feature and when its combine with scriptaculous it brings awesome javascript effects to your rails app.
Having use both prototype and jquery, i find jquery little less verbose.
After all, write less and do more should be given more preference.

Here are steps to replace prototype with jquery using a gem christened 'jquery-rails' .

In your  GemFile  add this one more gem


gem 'jquery-rails'

Rails partials ,layout and helpers

partials:
We can think of Rails partial templates (partials for short) as a kind of method
for views. A partial is simply a chunk of a view in its own separate file. You can
invoke (render) a partial from another template or from a controller, and the
partial will render itself and return the results of that rendering. And, just as
with methods, you can pass parameters to a partial, so the same partial can
render different results.


render(:partial => "partialFile")

html_escape in RoR

You can escape html entities from your code using Rails html_escape() method.

<%= html_escape ("<p> will remain as it is.")  %>
OR
<%=h  "<p>will remain as it is." %>

h() is a short tag for html_escape.
BUT IN RAILS 3 html_escape is used by default. 

Thursday 28 April 2011

How to create a checksum of a file in ruby?

Ruby Digest class does support many hashing algorithm. For example  sha1, sha2,sha256, md5 etc etc.
Md5 is most commonly used and it's known to be less secure than sha1.
Recently sha1 has been decoded by someone.
so, i prefer to use SHA2. :D

Sunday 24 April 2011

Gvim: best editor for linux workstations

I'm a big fan of TextMate text editor on Mac machine and on window i prefer Notepad to every other ide including eclipse. But nowadays i'm on linux and so i couldn't use textmate anymore and same goes for notepad++. I tried running it via wine, but most of its function didn't works well.
I google and found vim is one of the best editor for linux workstations. After trying a while, i found vim easy to use and rich in features.

First thing first, get yourself a copy of vim editor.  Vim comes in two variant : command line and Gui. I prefer Gui over command line one, reason being easier to edit and maintain while working on large project.

Saturday 23 April 2011

Upcoming Articles

*configuring and installing Full Text Seach Engine on Ruby on Rails, on Chantu sorry, Ubuntu. :P
*Top 10 rarely used  Mysql commands.
*

ProFTPD behind the NAT

Having installed proftpd on your system, let make it accessible to the world or atleast to your dear ones.

Edit your proftpd.conf file and uncomment or Append these lines

Port 21
PassivePorts 60000 65535
MasqueradeAddress YourSiteName.ath.cx
eg

ProFTPD(File Transfer Protocol) setup on Linux

Installation
$ sudo apt-get install proftpd

root@dragonaider:/home/sahilsk# sudo apt-get install proftpd
Reading package lists... Done
Building dependency tree       
Reading state information... Done .....


RoR heroku deployment

Heroku
Heroku is best known for Ruby On Rails hosting. It's world class service place it apart from others.

Best thing about Heroku is that it also provide a free hosting of 100mb diskspace and 5mb of Database.
Enough to get you started with ror project deployment.

since creating account on heroku is free you can also create create and deploy facebook on heroku for free.

Here are the step:

Network File System v4:setup

Quick setup of network file system on localhost:

Download following packages:

for server:
$ sudo apt-get install nfs-kernel-server

for client:
$ sudo apt-get install nfs-common

once done installing, configure your /etc/exports file. Here's mine:

Tuesday 19 April 2011

RMagick Installation And Usage

You might have heard of imagemagick libarary : the popular library that does almost all operation on image one can think of.
I'm a die hard fan of image magick library. and probably that's why no matter which language i choose to program i always opt imagemagick wrapper functions  for eg. Imagick for php, Magick++ for c++ and RMagick for Rails.

RMagick Installation :

$ gem install RMagck 2  # if Rails < 2 , install RMagick 1 else for Rails  3 install RMagick 2.x

confirm
$ gem list  # if you see RMagick in your list then it's installed in rubygems.


Rmagick installation on Windows

1) Go and download  rmagick for windows (win32)
Project page:
http://rubyforge.org/frs/?group_id=12&release_id=39888

Download here (RMagick-2.12.0-ImageMagick-6.5.6-8-Q8.zip)

2) Extract and CD to unzipped directory

3) Install it

gem update --system


You should see:
  • Successfully installed rmagick-2.12.0-x86-mswin32
If instead you get the message
  • Error installing gem rmagick[.gem]: buffer error
then you didn't update RubyGems. Do so now and re-run the gem install command.





downspout: download files over http and ftp protocols.

DownspoutA beautiful gem that let you download files from any website or any ftp site.
Here's quick example of downspout.

Installation :
$ gem install downspout
Once done, run the rails console
$ rails c



Operations on String :Ruby Way

I  want this post to filled with examples. But since i don't have much examples i urges you to please post some examples via your precious comments and help this post to grow more and more.

Well, more the merrier. 


To generate Timestamp like this :  20110419230024

some useful rails methods and links for sahilsk.

Rails Inflector: 
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html


if model name = "UserAccountHistory "  ,table name = "user_account_histories"
"user_History".tableize   # user_histories
"user_history".pluralize # user_Histories
"user_account_history".camelize # UserAccountHistory

Kaminari : beautiful pagination gem for Rails 3

Pagination on ActiveRecord


Pagination
In older times, i used to use Will_Paginate , a very nice pagination gem. But after using Kaminari i find will_paginate little more verbose.
Nowadays i uses  a beautiful gem christened Kaminari by amatsuda

How to setup kaminari to paginate your table ?

RoR : naming Conventions

controllers
name should be Plural eg.  users, items, teachers
multiwords name should be in  CamelCase,
eg.   UserItems,  UserSessions etc


model
name should be singular and database table name should be plural.
eg. table name : users  ; model name => user
table name :people ; model name => person


Got Lost? How to generate plural form for  table name?
ruby-1.9.2-p0 > "person".tableize
 => "people" 

learn more on text transformation a.k. Text Inflectors 


Table Relationships:
HINT: read it aloud

belongs_to :singularName
has_many :pluralNames
has_on :singularName





A very wonderful  post on RoR naming conventions is posted on  iT signals

How to install Rails in ubuntu?


Once you done installing ruby, lets connect to the next link in the chain :RAILS Installation.
Ruby programs comes in small modules called gems, which are self-contained packages of Ruby Code.
Since gems with different version numbers may cause some conflict, it's preferable to separate gemsets, which are self-contained budles of gems or simply Repository of Ruby Extensions.

So, to create two different gem sets one for each rail version do the following:
 $ rvm --create 1.8.6@rails2 #  or $ rvm --create 1.8@chantu
 $ rvm --create use 1.9.2@rails3 # or $ rvm --create 1.9.2@bhantu

first command create the gemsets rails2 associated with Ruby 1.8.6 while the second command creates the gemsets rails3 associated with Ruby 1.9.2 and uses it at the same time.

To use Ruby 1.9.2 and Rails 3 by default :
$ rvm --default use 1.9.2@rails3 # This sets the default Ruby to 1.9.2 and default gemset to rails3

Got Lost?
$ rvm --help
$ rvm gemset --help


Insall RubyGems
RubyGems: Package Mannager for Ruby projects. You can access those tons of ruby/rails extension to further enhance the productivity of your rails app had you have this.
So, lets get it first.

wait....
since you've installed RVM , you already got it. Bingo...
confirm:
sahilsk@dragonaider:~/cronfarm$ which gem
/home/sahilsk/.rvm/rubies/ruby-1.9.2-p0/bin/gem

 If you don't have it, go and download RubyGems, extract it and then go to the rubygems directory and run the setup file
$ [sudo] ruby setup. rb  # here sudo tag is optional

update your repo : RubyGems
$ gem update --system


Finally the moment you all waited so long  . Yes, Installation of RAILS
$ gem install rails --version 3.0.0 # or simple || $ gem install rails  || # gem will get the latest version for you.
 Confirm:
$ rails v
Rails 3.0





How to install Ruby on Ubuntu powered systems

Here's the first step to teach your system how to interpret ruby's methods and classes.
slap1 =>Install Ruby Interpreter.

The best and easiest way to install ruby interpreter in your system is by using 'Ruby Version Manager" a.k.a RVM. So, go and install it right away
Once done installing, just run through this commands. :

$ rvm get head  # Make RVM to go and get the latest records
$ rvm reload     #once done fetching latest records, update local records. NOTE: installation not yet started.
$ rvm list known # List Ruby interpreters available or installation
$ rvm list #lit Ruby Interpreters you've already installed

# START INSTALLING RUBY
$ rvm install 1.8.7-p174 # One of the interreters selected from the  (rvm list known) list
$ rvm install 1.9.2  # I'll install 1.9 version too. Why?? bcoz i'm kew........l. Not necessary for you. Opt the verson you like most.

Now, out of various version i've installed which one should i make default?? 1.8 or 1.9 ?or should i use any ruby interpreter??
If answer to any of above question is TRUE(YES) proceed
$ rvm use 1.9.2  # user ruby 1.9.1 OR simply  "rvm 1.9.2"

confirm  your ruby version by running this command in shell.
$ ruby -v

Still need help
$ rvm --help

$rvm gemset --help 


System wide install is no longer exist.
Use this command in Ubuntu console to install rvm as root and be available for all users:

it'll install your rvm in /usr/local/rvm .

now, add  this line in your ~/.bashrc file



change the line that goes like  [-z "$PS1"] .. to this


if [[ -n "$PS1" ]]; then
..
.....
Now, in the last, append these lines


[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"


fi 


last 'fi' to end the upper if block. :D



Hello World

=begin
A simple 'Hello World' Program in Ruby.
=end


ruby-1.9.2-p0 > def say_hello; puts "Hello World!"; end

ruby-1.9.2-p0 > say_hello
Hello World!