Tuesday, November 29, 2011

Setup project from Heroku and git repository

1) Install gem heroku
gem install heroku

2) Add heroku key
The first time you run the heroku command, you’ll be prompted for your credentials. Your public key will then be automatically uploaded to Heroku.
heroku keys:add
Generating new SSH public key.
Uploading ssh public key /home/sapna/.ssh/id_rsa.pub

3)git clone git@heroku.com:giraffe-dev.git

Tuesday, November 15, 2011

Kill Webrick process running on port 3000 without daemon

root@root:~$ sudo netstat -anp | grep 3000
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN      3023/ruby     
root@root:~$ sudo kill -9 3023
root@root:~$ rails s

Wednesday, November 9, 2011

Mysql - How to search for exact word match using REGEXP?

If you are looking to match EXACT words then don't use LIKE keyword.
You could do word boundaries with the following REGEXP. 
SELECT * FROM TABLE WHERE field_name RLIKE "[[:<:]]foo[[:>:]]";
In Ruby code you can use it like
Model.find(:all, :conditions => "field_name RLIKE 'foo.*'")
or
Model.find(:all, :conditions => "field_name REGEXP 'foo.*'")

Thursday, November 3, 2011

Javascript to check uncheck all checkbox in a Table

<a href="#" id="check_<%= id %>" onclick="checkParent('<%= id %>', true); return false;">All</a>

<a href="#" id="uncheck_<%= id %>" onclick="checkParent('<%= id %>', false); return false;" style="display:none;">All</a>

<table id="table_<%= id %>">
  <tr>
    <td>
      <%= check_box_tag "locations[]", location.id, false, :onchange => "validate();" ,:id => "locations", :class => "location" %>
    </td>
  </tr>
</table>

function checkByParent(aId,aChecked) {
   
    var inputs_in_table = document.getElementById("table_"+aId).getElementsByTagName("input");
    for(var i=0; i<inputs_in_table.length; i++)
    {
        if(inputs_in_table[i].type == "checkbox") inputs_in_table[i].checked= aChecked;
    }
    if (aChecked == true)
    {
      document.getElementById("check_"+aId).style.display = "none";
      document.getElementById("uncheck_"+aId).style.display = ""; 
    }
    else if(aChecked == false)
    {
      document.getElementById("check_"+aId).style.display = "";
      document.getElementById("uncheck_"+aId).style.display = "none"; 
    }
  }