Saturday 20 April 2013

ROR installation through RVM, along with passenger

Assumption:
I assume you have apache2 installed and working on your system/server.

Installtion instruction:
  1. Install rvm
  2. Install ruby with rvm and then install gems: Rails
  3. Install Passenger and passenger apache module
  4. Configure apache2
  5. Learn to create virtualhost settings for your every rails application

Installation of RVM and Ruby

Install rvm with ruby

Saturday 13 April 2013

Rails Validators


Validators in Rails 3:



How to build Custom validators in Rails 3?



def validate 
  errors.add_to_base "If you are attaching an image you must enter a title for it" if         
  !upload_image.blank? and image_label.blank? 
end 

Note: add_to_base is deprecated in Rails 3 and you'll get "undefined method add_to_base ".   So, instead use :

        errors[:field_name] << "Error Message"

Form Fields in Rails



form_for         :


<%= f.select :menu_id, options_from_collection_for_select(@menus, 'id', 'name') %>
<%= f.datetime_select  :schedule , :prompt => {:day => 'Choose day', :month => 'Choose month', :year => 'Choose year'}%>


form_tag         :


<%= form_tag :controller => "index", :action => "index" do %>
<%= select_tag(:area_id, options_from_collection_for_select(@areas, :id, :area_name, @area_id),
 :onchange => "this.form.submit()") %>
<% end %>


OR
Over an integer range:
<%= f.select :field_name, (1..50) %>  OR select("product", "quantity", 1..100)


<form accept-charset="UTF-8" action="/area/2" method="post">
    <select id="area_id" name="area_id" onchange="this.form.submit()">
      <option value="1">Japan</option>
      <option value="2" selected="selected">Thailand</option>
      <option value="3">USA</option>
      <option value="4">Canada</option>
      <option value="5">England</option>
    </select>
</form>



:Value parameter

Error sometime occur: undefined method 'merge' for  xxx:xxx

Solution: use ":value" parameter


<%= f.hidden_field :session_id,  :value => "abcdefg0123456789" -%>


:MULTIPLE parameter
When you need to send an array of information back
<%= f.hidden_field "orders", :multiple => true,  :value=>schedule.id %>

Install php5 and mysql powered by nginx

Trouble shooting Rails



Errors:

------------------------------------------
------------------------------------------
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.
rb:2:in `require': Incorrect MySQL client library version! This gem was compiled
 for 6.0.0 but the client library is 5.5.16. (RuntimeError)
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/m
ysql2/mysql2.rb:2:in `<top (required)>'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/m
ysql2.rb:9:in `require'
....

Solution:
   change 'mysql' version accordingly. Newer('older') version of mysql gem might have installed.

As in my case it's 0.3.x installed .  I changed it to 0.2.x and now my app is running perfectly.

In gem file, make following changes:

                     '0.2.gem 'mysql2', '0.2.6'

------------------------------------------
------------------------------------------

root@m1:/var/www/production# heroku keys:add
/usr/local/lib/ruby/1.8/x86_64-linux/readline.so: libreadline.so.5: cannot open shared object file: No such file or directory - /usr/local/lib/ruby/1.8/x86_64-linux/readline.so (LoadError)
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:55:in `require'
        from /usr/local/lib/ruby/gems/1.8/gems/heroku-2.32.14/lib/heroku/command/run.rb:1
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:55:in `gem_original_require'
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:55:in `require'
        from /usr/local/lib/ruby/gems/1.8/gems/heroku-2.32.14/lib/heroku/command.rb:14:in `load'
        from /usr/local/lib/ruby/gems/1.8/gems/heroku-2.32.14/lib/heroku/command.rb:13:in `each'
        from /usr/local/lib/ruby/gems/1.8/gems/heroku-2.32.14/lib/heroku/command.rb:13:in `load'
        from /usr/local/lib/ruby/gems/1.8/gems/heroku-2.32.14/lib/heroku/cli.rb:27:in `start'
        from /usr/local/lib/ruby/gems/1.8/gems/heroku-2.32.14/bin/heroku:17
        from /usr/local/bin/heroku:23:in `load'
        from /usr/local/bin/heroku:23

Solution:
  $ apt-get install libreadline-ruby1.8

understand recursion

In my school days i was afraid of a thing called Recursion. For me it works like a magick. I never try to take recursion problem head on and worked on them. Instead i take shelter to conditional loop and for few years it worked fine, untill i faced one problem.

Problem was specific to my job but to put it simple it was a problem of traversing a tree and to tell generate its  position matrix.


            A
          /     \
       B        C
      /   \       /  \
    D    E   F    G

Problem:  List down all the node along with their relative position to Root 'A'
eg.
A:  0  (Assume A's position as 0 :root)
B : 0 0
C: 0 0 1
D: 0 0 1  .




There is wonderful Article about recursion on stackoverflow. I strongly recommend it for those who want to see the real logic behind this magick. 

http://stackoverflow.com/questions/717725/understanding-recursion

Rails Task: Creation and deployment

Rails does have one very interesting module, besides dozens of other : RAKE

Rake help you write task and then you can use it to write schedulers or database maintenance script or anything, as per your requirement.
I used it to write one cron job.
Here is how i made one rake task.

$ rails generate task   namespace_name taskName_one taskName_two
 ==> It will generate file with the name  namespace_name.rake and in there it will define two methods
with the name "taskName_one" and "taskName_two".