Skip to content

OpenERP Developer, E-ndicus Infotech

October 23, 2011

Last september 19,  i joined as OpenERP Developer  at E-ndicus Infotech Pvt Ltd Chennai http://e-ndicus.com/

now i’m working in python to devlop openERP

Web Scraping in Ruby

May 12, 2011

Hpricot is a HTML parser, fantastic ruby library, easy to install and easy usage

To install

sudo gem install hpricot open-uri

open-uri is using a network streams

here i posted a simple web scraping code

This code to fetch the group of student results from the Annauniversity website


# Fetch my class students exam result from AnnaUniversity site
# Progamme name scrabing_exam_results.rb
# Author : Rajkumar.S
# version : 0.01
# License: GNU GPL 3

require 'rubygems'
require 'open-uri'
require 'hpricot'

url = "http://result.annauniv.edu/cgi-bin/result/re10.pl?regno="
# exam_no is a range
exam_no = "52108621001".."52108621039"

exam_no.each do |each_number|
doc=Hpricot(open(url+each_number))
data=doc.search('table')
# write a file as html format easily view all results in one page
File.open("result.html","a") {|f| f.puts(data)}
# find the inside content of table tag
x=doc.search('table').inner_html
# it is remove the html tags
a=x.gsub(/<\/?[^>]*>/,"")
# spearate an array where \n is placed
b=a.split.join("\n")
puts b+"\n"+"======================="

File.open("result.txt","a") { |f| f.puts(b+"\n\n"+"=================")}

end

Google maps in rails-3

April 15, 2011

Using a gmaps4rails gem to display a map in our application

create a rails application

$ rails new gmaps -d mysql

then configure mysql  password

In Gemfile add a gem as ==== gem ‘gmaps4rails’

and bundle it

create a scaffold  $ rails g scaffold Location name:string address:string longitude:float latitude:float.

remove the longitude and latitude fields in _form.html.erb

After that, add this in layout

<%= yield :head %> (in your header) <%= yield :scripts %> (in your footer)

/app/views/layouts/application.html.erb

<html>
<head>
  <title>Googlemap</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
  <%= yield :head %>
</head>
<body>
<%= yield %>
<%= yield :scripts %>
</body>
</html>

In model /app/models/location.rb to add

class Location < ActiveRecord::Base
 acts_as_gmappable
      def gmaps4rails_address
          address
      end
       def gmaps4rails_infowindow
         "<h4>#{name}</h4>" << "<h4>#{address}</h4>"
     end
end

add the line in controller /app/controllers/locations_controller.rb

def index
    @locations = Location.all
    @json = Location.all.to_gmaps4rails
    ..........
    ..........

In your view app/views/locations/index.html.erb to add a last line as

    <%= gmaps4rails(@json) %>

then start the server to put the name and address, longitude and latitude as set automatically,
then will be show the markers as corresponding address and click the marker icon
it will shows infowindow as name and address

Sample in heroku: http://hrgmaps.heroku.com/

Rails 3: Nested model form(line item) using jquery and Ajax

March 23, 2011

Nested model form(line item) : நெஸ்டட் மாடல் பார்ம் என்பது ஒரு படிவத்தினுல் இன்னொறு மாடலில் உள் ள field -யை உள்னுழைப்பதாகும்.

முந்தைய பதிவினில் (post) நாம் ஒரே படிவத்தில் உள்ள field -யை hide செய்வதையும் show செய்வதையும் பற்றி பார்த்தோம்.

தற்போது line item எப்படி செய்வது என்பதை பற்றி காண்போம்.

முதலில் கீழ் வரும் கட்டளையை பயன்படுத்தி ரெயில்ஸ்-யில் ஓர் project -யை உருவாக்கவும்

$ rails new lineitemtest -d mysql

பின்னர் config/database.yml -ல் தங்களது mysql கடவுச்சொல்லை அதற்குரிய இடத்தில் பதிவுசெய்யவும்.

Interview மற்றும் Applicant என்ற இரண்டு மாடல்களை scaffold பயன்படுத்தி பின்வரும் கட்டளைகள் கொண்டு உருவாக்கவும்

$ rails g scaffold Interview name:string interview_date:date

$ rails g scaffold Applicant name:string interview:references

இவற்றிற்கான database -னை உருவாக்கவும்

$ rake db:create

$ rake db:migrate

இந்த project-ல் jquery -யினை பயன்படுத்த உள்ளதால் அதற்கான மூல நிரலை இங்கே நகலெடுத்து jquery-1.5.js என்ற கோப்பினை உருவாக்கி அதனுல் ஒட்டி /public/javascripts -ல் பதிவு செய்யவும். பின்பு /app/views/layouts/application.html.erb -ல் கீழ் உள்ளவாறு மாற்றம் செய்யவும்.

<%= stylesheet_link_tag :all %>
<%= javascript_include_tag 'jquery-1.5.1' %>
<%= csrf_meta_tag %>

app/models/interview.rb -ல் ஒன்றுக்கு மேற்பட்ட nested applicants ஏற்றுக்கொள்ள

has_many :applicants
accepts_nested_attributes_for :applicants

app/models/applicants.rb

class Applicant < ActiveRecord::Base
belongs_to :interview
end

என்ற வரியை பதிவு செய்யவும்

நம்முைடய interview form-ல் line item -ஐ  கெண்டு வருவதற்கு கீழ்கண்ட நிரல்களை app/view/interviews/_form.html.erb -ல் submit div class ற்கு மேல் உள்ளிடவும்.

<div class="item">
<%= add_applicant_link ("add freshers") %>
</div>
<div id="result">
</div>

மேலுள்ள add_applicant_link க்கிற்கான function-னை interview_helper-ல் எழுதவும்.

/app/helpers/interview_helper.rb -ல்

module InterviewsHelper
def add_applicant_link(person)
link_to person, "#", :class => "add_applicant_class"
end
end

மேலுல்ல class -யினை பயன்படுத்தி புதிதாக jquery -யை public/javascripts/firstscript.js  உருவாக்கி நம்முடைய நிரலை கீழ் உள்ளவாறு எழுதவும்.

$(document).ready(function(){
$('.add_applicant_class').click(function(){   //in interview helper
  $.ajax({
  type:'POST',
  url:'add_applicants',  data:'',
  success: function(data){
  $(data).appendTo('#result');
  }
})
});
});

நம்முடைய firstscript.js jquery-யினை application-வுடன் இணைக்க app/views/layouts/application.html.erb-ல் கிழ் உள்ளவாறு இனணக்கவும்.

<%= stylesheet_link_tag :all %>
<%= javascript_include_tag 'jquery-1.5.1', 'firstscript' %>
<%= csrf_meta_tag %>

ajax-ல் கூறிய add_applicants url, controller-யை கூப்பிடுகிறது ஆகையால் app/controllers/interview.rb -ல்
கீழுள்ள funtion னை உள்ளிடவும்

def add_applicants
render :partial => 'applicants', :locals => {:applicant_object => Applicant.new}
end

render :partial க்கான view-வை புதிதாக app/views/interview-ல் _applicants.html.erb என்று உருவாக்கி கீழ் உள்ள நிரலை இடவும்.

<div class="field">
<%= fields_for "interview[new_applicant_attributes[]]",applicant_object do |applicant_form| %>
<%= "applicant" %>
<%= applicant_form.text_field :name %>
<% end %>
</div>

மேலே பெறப்பட்ட new_applicant_attributes[]-ஐ database-ல் பதிய app/models/interviews.rb-ல் புதிய funtion-ஐ கீழ் உள்ளவாறு எழுதவும்.

class Interview < ActiveRecord::Base
has_many :applicants
accepts_nested_attributes_for :applicants
 def new_applicant_attributes=attributes
attributes.each do |record|
applicants.build(record)
end
end
end

add_applicants-ஐ வெளியீட்டில் காண்பதற்கு config/routes.rb-ல் வழி(path)யினை கூற

resources :applicants
match "interviews/add_applicants" => "interviews#add_applicants"
resources :interviews

வெளியீட்டினை காண்பதற்கு
$ rails server  என்ற கட்டளையை முனையத்தில் உள்ளிடவும்.
show வினை சொடுக்கினால் உள்ளிடப்பட்ட அனைத்து applicants-ஐ காண app/views/interviews/show.html.erb-ல் கீழ் உள்ள நிரலை எழுதவும்.

<p>
<b>Applicants:</b>
<ol>
<% for applicant in @interview.applicants %>
<li><%= applicant.name %></li>
<% end %>
</ol>
</p>

Rails 3: jquery basics

March 22, 2011

I created the  jquery using simple rails 3 application

$ rails new jquery_example -d mysql

At first we copy the jquery uncompressed file here and create a file in public/javascripts/jquery1.5.js  and paste it. This is a base file of jquery

then

$ gedit config/database.yml

put your password as corresponding field

$ rails g scaffold Student name:string standard:string mark:integer status:boolean description:string

$ rake db:create

$ rake db:migrate

go to app/views/students/_form.html.erb and edit it as follows,  this id’s and class are using jquery for client side validations


<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :standard %><br />
<%= f.text_field :standard %>
</div>

<div class="field">
<%= f.label :mark %><br />
<%= f.text_field :mark, :id => "mark_id" %>
</div>
<div class="field">
<%= f.label :status %><br />
<%= f.check_box :status, :id => "status_id" %>
</div>

<div class="description" style="display:none">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="filed">
<%= f.submit %>
</div>

we write our own jquery code in public/javascripts/myscript.js and save it

$(document).ready(function(){
$('#mark_id').click(function(){
alert ('carefully enter the mark');
});
$('#status_id').click(function(){
if ($('#status_id').attr('checked'))
{$('.description').show();
}
else
{
$('.description').hide();
}
});
});

we must given a path of our jquery files unless its not working so
app/views/layout/application.html.erb in this file to remove :defaults jquery had prototype and enter our file names
as below

<%= stylesheet_link_tag :all %>
<%= javascript_include_tag 'jquery-1.5', 'myscript.js' %>
<%= csrf_meta_tag %>

to run our application

status check box is ticked it shows the description field otherwise it was hidden, because before we mention style=”display:none” in _form.html.erb

 

Relationship concepts in rails 3

March 10, 2011

Mr. Thayagarajan taught  relationship between the models

Relationships are

has_many ,  belongs_to ,  has_and_belongs_to_many ,  has_many through

rails script  is easy to write because it has english grammar

1. has_many, belongs_to

Example :   we take a company , A Company has many  Employees , Employees are belongs to Company

using this sentance to write relationship script in model

app/model/company.rb


class Company < ActiveRecord::Base

has_many :employees

end

app/model/employee.rb


class Employee < ActiveRecord::Base

belongs_to :company

end

2. has_and_belongs_to_many

Example :  Bank has many Customers and also Customer has many Banks (accounts)

app/model/bank.rb

class Bank < ActiveRecord::Base
    has_and_belongs_to_many :customers
end

app/model/customer.rb
class Customer < ActiveRecord::Base
  has_and_belongs_to_many :banks
end

in bank_customer_join migration table we define the conditions
db/migration/2011….bank_customer_join.rb
class bank_cutomer_join < ActiveRecord::Migration
    def self.up
        create_table :bank_customer_join, :id => false  do |t|
            t.integer :bank_id
            t.integer :customer_id
            t.timestamps
        end
    def self.down
    .........
end
end

In this join table has only the two way join table and without table id, this relationship does not allow the third one
3. has_many through
it makes a n way joint table, easy to retrive the info’s or data’s in that joint table
create a rails application
and create  a models
$ rails g model Applicant name:string address:string
$ rails g model Mandate name:string
$ rails g model Interview applicant_id:integer mandate_id:integer description:string     #it is a joint table of applicant and mandate tables
app/models/applicant.rb
class Applicant < ActiveRecord::Base
  has_many :interviews
  has_many :mandates, :through => :interview
end

app/models/mandate.rb
class Mandate < ActiveRecord::Base
  has_many :interviews
  has_many :applicants, :through => :inerview
end<

app/models/interview.rb
class Interview < ActiveRecord::Base
  belongs_to :applicant
  belongs_to :mandate
end

$ rake db:create
$ rake db:migrate
$ rails console
in rails console we insert a applicant data’s
use the above steps again and again to insert more data’s via console
insert mandate data’s
insert interview data as the joint table
In Databases

Enhance the year in rails drop down list

February 17, 2011

In rails3 application, we create a scaffold in any date it display the limited years in date drop down list

enhance the year and fix the years what you want

to edit  a code in  apps/views/……/_form.html.erb


<%= f.date_select :purchased_on, :start_year => Time.now.year - 40, :end_year => Time.now.year + 10 %>

Follow

Get every new post delivered to your Inbox.