Wednesday, December 15, 2010

Adding validations to Rails Appliaction

There are many ways to give validations in ruby on rails application.
One of the project I was working on needed something like this.
The fields in the database were cheque:boolean, cheque_no:integer, cheque_date:date, bank_name and other.
I wanted that without entering cheque_no, cheque_date and bank_name the user is not suppose to select the just checkbox of :cheque and update the record,
this can be done through javascript only by hiding the fields.
But the fields should be always visible.
So needed some nice validation over those fields.
the boolean cheque field can only get updated when the user enters other 3 fields.

So In model

validates_presence_of :cheque_no, :cheque_date, :bank_name , :if => :validate_presence_of_cheque?

# ----------- for adding validation on cheque_no, cheque_date, bank_name
def validate_presence_of_cheque?
cheque == true
end

I added the field as there were some more boolean fields on that record.

There are many ways to give validations in ruby on rails application.
But how many of us knows all of them.

The best place to find those is guides.rubyonrails.org.
Where we can get list of validations with examples.

Sunday, October 31, 2010

Finding out operating system through javascript

For finding out operating system through javascript you will need to add

<script type="text/javascript">
  function findOS (  )  {
    var ua = navigator.userAgent.toLowerCase (  ) ;
    if  ( ua.indexOf ( "win" )  != -1 )  {
      return "Windows";
    } else if  ( ua.indexOf ( "mac" )  != -1 )  {
      return "Macintosh";
    } else if  ( ua.indexOf ( "linux" )  != -1 )  {
      return "Linux";
    } else if  ( ua.indexOf ( "x11" )  != -1 )  {
      return "Unix";
    } else {
      return "Computers";
  }
}
</script>

in the head section and in body just add :

<script type="text/javascript">
  document.write ( findOS (  )  ) 
</script>
for more solutions you can visit java2s.com

Wednesday, October 20, 2010

Tabs with simple Javascript

Making tabs with css is very simple and also getting the information after clicking that tab is simple, but the problem arrives when the styling of that tab should clearly state that the tab is the current tab.
I was not able to get that with css.
So I tried to find the solution but then decided to make my own code as it was a static site and I was not going to add those on the fly in the program.

I just wanted 3 tabs which will never change.
So

In view

--Head section

<script src="prototype.js" type="text/javascript"></script>
<script src="tabs.js" type="text/javascript"></script>

--Body section

<div>
<div>
<a href="#" id="first_tab" onclick="firsttab();" class="tabs"/> First Tab </a>
<a href="#" id="second_tab" onclick="secondtab();" class="tabs"/> Second Tab</a>
<a href="#" id="third_tab" onclick="thirdtab();" class="tabs"/> Third Tab </a>
</div>

<div id="container" >
<div id="first_tab_div"> some text for first div </div>
<div id="second_tab_div"> some demo text for second div </div>
<div id="third_tab_div"> some text for third div </div>
</div>
</div>

In tabs.js

function enquiry(){
 $('first_tab_div').show(); 
 $('second_tab_div').hide(); 
 $('third_tab_div').hide(); 
 $('first_tab').style.backgroundColor='#E3E3D9'; 
 $('second_tab').style.backgroundColor='#FFFFF4'; 
 $('third_tab').style.backgroundColor='#FFFFF4'; 
}
function meeting(){
 $('second_tab_div').show(); 
 $('first_tab_div').hide(); 
 $('third_tab_div').hide(); 
 $('second_tab').style.backgroundColor='#E3E3D9'; 
 $('first_tab').style.backgroundColor='#FFFFF4'; 
 $('third_tab').style.backgroundColor='#FFFFF4'; 
}
function advertisement(){
 $('third_tab_div').show(); 
 $('first_tab_div').hide(); 
 $('second_tab_div').hide();
 $('third_tab').style.backgroundColor='#E3E3D9'; 
 $('first_tab').style.backgroundColor='#FFFFF4'; 
 $('second_tab').style.backgroundColor='#FFFFF4'; 
}

In CSS

a#first_tab, a#second_tab, a#third_tab,
a#first_tab:visited, a#second_tab:visited, a#third_tab:visited{color:#000; 
padding:8px; 
height:25px; 
line-height:25px; }
a#second_tab, a#third_tab, a#second_tab:visited, a#third_tab:visited{background:#fffff4}
a#first_tab, a#first_tab:visited, a#first_tab:hover, a#second_tab:hover, a#third_tab:hover{background:#E3E3D}
#container{background:#E3E3D; margin:0}

That's It. The simple and really quick tabs are ready to use.
But this code is only useful when we are not suppose to add any tabs and you want to add heavy tabs creators javascript files.
This code is not useful when the program is going to create any. For that there are many sources available::

webdesignbooth Or wittysparks...

Tuesday, February 23, 2010

Installing Sphinx for ubuntu

I installed Thinking Sphinx by following the step posted in hackido.com
This helped me alot simple and fast installation.
For mysql the thinking sphinx was running cool, but there was a problem I faced as we were not using mysql, we were using postgresql for one of our project.
So I followed the post by freelancing-god.github.com.
./configure --with-pgsql
./configure --with-pgsql=/usr/local/include/postgresql
pg_config --pkgincludedir
make
sudo make install

And then I indexed the Thinking Sphinx for project and started the same, which was successfully started.

Monday, February 22, 2010

Installing netbeans on Ubuntu

Installing netbeans on Ubuntu is very easy. I installed it on Karmic(Ubuntu 9.10)
For netbeans we need to add sun-java6-jdk and the command for that is
sudo apt-get install sun-java6-jdk

Once the sun-java6-jdk is installed we then start to install netbeans and there are 2 steps for it, both are very easy.
First just with one command which is
sudo apt-get install netbeans
or even we can install that from Synaptic Package Manager. Just search for netbeans which will also find dependencies and install tha same what ever required. The problem I faced for this installation was that I didnt get the latest version of netbeans.
Second is we go to the netbeans.org download the netbeans from download page. This page gives you the list of technologies for which you need the software. Just see which ever you need. For me it was for ruby.
Once you are complete with download go to the folder where your .sh file of netbeans is downloaded and then type
chmod +x (filename).sh

Once the file is being extracted go to the Home folder. There we can find the netbeans-(version_no) for me it was netbeans-6.8. After that go in the bin folder and run
./netbeans

This will start the netbeans and will also create a desktop shortcut.

Tuesday, February 16, 2010

Using Fastercsv to import data

Fastercsv is best for importing data in ruby on rails application.
For this we just need to add the gem.
gem install fastercsv