Pages

Thursday, March 31, 2011

What is Mean By Ruby on Rails

   it's a two part one is"Ruby" and another one is "Rails"

Ruby:

  •   it's a object Oriented programming language
  • created in 1995 'YUKIHIRO MATSUMUTO' ('matz')
  • used for many purpose, not just web applications
 Rails:

  • open source, web application framework
  • written in Ruby
  • created in 2003 by 'DAVID HEINEMIER HANSSON' ('DHH')
  • created as foundation of 37 signals Basecamp and then released as open source in 2004
Framework:

    a set of code Libraries [control the program] and Data structures that provide generic functionality

Why Use Ruby on Rails:

      object oriented, easily readable, unsurprising Syntax and behavior

Dry Code:

Donot repeat yourself, consistent code that is easily to maintain

Convention Over Configuration:

  • sensible default, only specify unconventional aspects
  • speeds development, less code to maintain follows best practice(optional)
Webserver:

  • Apache 1 or 2
  • passenger |mod_rails |
  • Nginx (Engine X")
  • Lighttp ("Lighty")
  • Mongral
  • webrick



Monday, March 21, 2011

Line Item (or) Nested Model

In this model we are using [jquery & Ajax],   Guided by Mr.Thyagarajan

jQuery :   is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML.[1] It was released in January 2006 at BarCamp NYC by John Resig.

 AJAX:

AJAX = Asynchronous JavaScript and XML.
AJAX is not a new programming language, but a new way to use existing standards.
AJAX is the art of exchanging data with a server, and update parts of a web page - without reloading the whole page.

 im learning how  work jquery and ajax in rails in this example is given below

first we create a rails folder

$ rails new land -d mysql

next we are entering a project directotry

$cd land/

edit the database.yml and enter the mysql password
$ gedit config/database.yml

create a scaffold  name& address:

$rails g scaffold Country name:string address:string
 
create a scaffold  name&references
 
$rails g scaffold State name:string country:references

create a scaffold city name & references;

$rails g scaffold City name:string state:references


create the database and migrate it:
$rake db:create
$rake db:migrate

before move the concept,  Jquery file download from http://jquery.com/ .copied to the public/javascript folder like jquery.js and we will create the first.js

     now how jquery is working , it will explained given below.
app/views/layout and then open the appliation.html.erb
   given the jquery folder name &power.js
 
 <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag 'jquery-1.5.1', 'power' %>
  <%= csrf_meta_tag %>
next we move to app/views/countries folder just open the _form.html.erb 
       
 <div>
<pre> <%= f.label :name %><br />
<%= f.text_field :name, :id => 'countries_name_id'  %>
</div>
open the javascript and enter the given code:
  $(document).ready(function(){
$('#countries_name_id').click(function(){
alert('check');
});
});
 start the sever [localhost:3000/countries]
then output will be given in 


 now we are going to create, under the folder _form.html.erb


 <div class='add states' >
  <%= add_state_link 'add_states' %>
  </div>
 <div id="result">
</div>


here 'add_state_link' is defined as helper class 
            app/helpers/countries_helper and then define it by

def add_state_link(name)
link_to name, '#', :class => 'add_state_class'
end

it will link as javascript  it contains [public/javascript] like this power.js

 $(document).ready(function(){
$('.add_state_class').click(function(){
alert('check');
});
});

the output is : 


                                                  

 it's working so we need ajax in nested model and then we define url in the success handler:
$(document).ready(function(){
$('.add_state_class').click(function(){
$.ajax({
url: "/countries/add_states",
method: 'POST',
data: '',
success: function(data){
$(data).appendTo('#result');
/*alert('checking');*/
}
});
});
});
start the sever and click add_state it's giving Routingerror like this:

                                           
   so rectify this error , go to config/ routes .rb and just paste the code like this

match "countries/add_states" => "countries#add_states"
resources :countries

next we are going to controller folder  app/controllers/countries_controller.rb
add like this:

def add_states
render :partial => 'states', :locals => {:state_object => State.new}
end

it will redirect the app/views/countries/_state.html.erb .  it will create our own:

<div>
<%= fields_for "country[new_state_attributes][]",state_object do |state_form|
%>
 <%= state_form.text_field  :name %>
<% end %>
 </div>

last step is redirected app/models/country.rb 

 class Country < ActiveRecord::Base
has_many :states
accepts_nested_attributes_for :states, :reject_if => lambda {|a| a.values.all?(&:blank?) }, :allow_destroy => true
def new_state_attributes=attributes
attributes.each do |each_record|
states.build(each_record)
end
end
end

and define the state.rb

class State < ActiveRecord::Base
  belongs_to :country
has_many :cities
accepts_nested_attributes_for :cities
end 

then output is like this: 

 one example is entering country name and state name in this example is:  

                                 

                          finally it will successfully created


 

 

Wednesday, March 9, 2011

Rails model relationship

       
                     Mr.Thyagarajan(sir) Took the class

Hi today i learning rails model relationship concept:

has_many,belongs_to: [habtm]
has_and_belongs_to_many:
has_many :through .

1.has_many,belongs_to:
             in this example is  more than employee working under the company
   
class Clinic < ActiveRecord::Base
has_many :employee
end
2.has_and_belongs_to_many:
               doctors have many patient , and patient have many doctors


class doctor < ActiveRecord::Base 
has_and_belongs_to_many :patient
end
         
 3.has_many :through :         
  This explanation is [has_many :through] concept

 im creating rails model relationship in mobile marketing(project)
  [MSR,clinic and doctor through (tour)program]

~$ rails new mmfa -d mysql
    first we are creating a rails

~$ cd mmfa/
   entering a project directory

~/mmfa$ rails generate model Clinic name:string address:string
    generate a rails in clinic

~/mmfa$ rails generate model Doctors name:string phone:string mail:string
  generate a rails in doctors

~/mmfa$ gedit config/database.yml
   edit the database and enter the password

~/mmfa$ rake db:create
~/mmfa$ rake db:migrate


~/mmfa$ rails generate model Tour clinic_id:integer doctor_id:integer
   tour is a bridge b/w clinic and doctor

~/mmfa$ gedit app/models/clinic.rb  app/models/doctor.rb  app/models/tour.rb
    edit three models

1.clinic.rb

class Clinic < ActiveRecord::Base
has_many :tours
has_many :doctors, :through => :tours 
end

2.doctors.rb

 class Doctor < ActiveRecord::Base
has_many :tours
has_many :clinics, :through => :tours
end

3.tour.rb

class Tour < ActiveRecord::Base
belongs_to :clinic
belongs_to :doctor
end

~/mmfa$ rake db:migrate 
next we are open a new tab entering a project directory

~/mmfa$ rails console
   entering a irb
  
 
 the example is given below
  example:

p=Clinic.new
=> #<Clinic id: nil, name: nil, address: nil, created_at: nil, updated_at: nil>
irb(main):003:0> p.name="bava"
=> "bava"
irb(main):004:0> p.save
=> true
irb(main):005:0> p=Clinic.new
=> #<Clinic id: nil, name: nil, address: nil, created_at: nil, updated_at: nil>
irb(main):006:0> p.name="kovai"
=> "kovai"
irb(main):004:0> p.save
=> true
 
entering a doctors name:
the example is given below
example:

p=Doctor.new
=> #<Doctor id: nil, name: nil, phone: nil, created_at: nil, updated_at: nil>
irb(main):022:0> p.name="mani"
=> "mani"
irb(main):023:0> p.save
=> true

irb(main):024:0> p=Doctor.new
=> #<Doctor id: nil, name: nil, phone: nil, created_at: nil, updated_at: nil>
irb(main):025:0> p.name="vadi"
=> "vadi"
irb(main):026:0> p.save
=> true


irb(main):036:0> p=Tour.new

=> #<Tour id: nil, clinic_id: nil, doctor_id: nil, created_at: nil, updated_at: nil>
irb(main):037:0> p.clinic = Clinic.find(3)
=> #<Clinic id: 3, name: "voc", address: nil, created_at: "2011-03-09 17:44:51", updated_at: "2011-03-09 17:44:51">
irb(main):038:0> p.doctor = Doctor.find(3)
=> #<Doctor id: 3, name: "kalai", phone: nil, created_at: "2011-03-09 17:52:01", updated_at: "2011-03-09 17:52:01">
irb(main):039:0> p.save
=> true
p=Tour.all

l> select* from tours;
+----+-----------+-----------+---------------------+---------------------+
| id | clinic_id | doctor_id | created_at          | updated_at          |
+----+-----------+-----------+---------------------+---------------------+
|  1 |         3 |         3 | 2011-03-09 17:54:22 | 2011-03-09 17:54:22 |
|  2 |         2 |         4 | 2011-03-09 18:06:14 | 2011-03-09 18:06:14 |
|  3 |         2 |         4 | 2011-03-09 18:07:16 | 2011-03-09 18:08:19 |
+----+-----------+-----------+---------------------+---------------------+
3 rows in set (0.00 sec)

mysql> select* from clinics;
+----+--------+---------+---------------------+---------------------+
| id | name   | address | created_at          | updated_at          |
+----+--------+---------+---------------------+---------------------+
|  1 | bava   | NULL    | 2011-03-09 17:44:02 | 2011-03-09 17:44:02 |
|  2 | gandhi | NULL    | 2011-03-09 17:44:34 | 2011-03-09 17:44:34 |
|  3 | voc    | NULL    | 2011-03-09 17:44:51 | 2011-03-09 17:44:51 |
|  4 | ramana | NULL    | 2011-03-09 17:45:14 | 2011-03-09 17:45:14 |
|  5 | kovai  | NULL    | 2011-03-09 17:45:40 | 2011-03-09 17:45:40 |
+----+--------+---------+---------------------+---------------------+
5 rows in set (0.00 sec)

mysql> select* from doctors;
+----+---------+-------+---------------------+---------------------+
| id | name    | phone | created_at          | updated_at          |
+----+---------+-------+---------------------+---------------------+
|  1 | mani    | NULL  | 2011-03-09 17:51:09 | 2011-03-09 17:51:09 |
|  2 | vadi    | NULL  | 2011-03-09 17:51:37 | 2011-03-09 17:51:37 |
|  3 | kalai   | NULL  | 2011-03-09 17:52:01 | 2011-03-09 17:52:01 |
|  4 | revathi | NULL  | 2011-03-09 17:52:29 | 2011-03-09 17:52:29 |
|  5 | devi    | NULL  | 2011-03-09 17:52:51 | 2011-03-09 17:52:51 |
+----+---------+-------+---------------------+---------------------+
5 rows in set (0.00 sec)




Saturday, February 12, 2011

Setting of root password in ubundu(linux)

Ubuntu has no password for root user as default.If you want to reset root password, you need to use sudo passwd command in shell. To Open shell, click Applications » Accessories » Terminal. In shell,type sudo passwd. It will ask you password. Type your login password.
venkat@venkat-desktop:~$ sudo passwd
Password:
And then it will ask you to reset password for root. Type it twice
Enter new UNIX password:
Retype new UNIX password:
After reset root password. It will show this message in Shell(passwd: password updated successfully).

Rails Installation

       Installation of  rails and mysql

step1:Ruby installation:
$sudo apt-get install ruby ruby-dev libopenssl-ruby1.8 irb ri rdoc

Step2:Install Mysql
 if mysql allready installed in your computer give the below command
$sudo apt-get install libmysql-ruby sqlite3

if mysql not install in your computer
$sudo apt-get install mysql-server sqlite3 libmysql-ruby

step3:some header files needs to install for compile
$sudo apt-get install build-essential libmysqlclient-dev libmysql-ruby libsqlite3-ruby libsqlite3-dev

Step4:Need gem install
$sudo apt-get install rubygems

Step5:install mysql,rails,sqlite3-ruby mongrel through rubygems
$ sudo gem install rails mysql sqlite3-ruby mongrel
if this show errors
$sudo apt-get install rails rails-doc rails-ruby1.8
then retry this
$ sudo gem install rails mysql sqlite3-ruby mongrel
Rails successfully install your system..
To check if is install
open terminal
$rails -v
Rails 3.0.3

   if you have any doubt in installation in rails and mysql  go throught the blog
    http://www.coachfactory.blogspot.com/  you can get clear idea about this

Thursday, January 20, 2011

Project Title : Mobile Marketing Force Automation

The description:

All the pharmaceutical industries marketing department has its marketing force asmedical representatives are spread across the country. It is difficult to provide each medical representatives a laptop for updating their marketing reports such as samples delivery todoctors, collecting stock requirements from the pharmacy for their product , schedules of marketing representatives for meeting the doctors,preparing the sales report .

MOBILE MARKETING FORCE AUTOMATION (MMFA) is the system which works on a mobile

phone (typically Nokia 6600) which has a browser (opera) and has connection to the server space which is hosted in the linux platform(Server management is tackkled by the third party hosting company).All the above said reports inputs are managed through the mobile phone. The mobile phone has the internet connection (provided by bsnl gprs or hutch service) which

cost from rupees 99/- to Rs 300/- per month.The RoR application has a specified cascading Style sheet(CSS)

which works on a mobile and separate Cascading case sheet to work on a pc for providing

inputs to system. The server space is taken from a hosting company which has a mysql/postgres

sql application.the RoR applicationresides in the server and it is been accessed by the

client(mobile phone) via internet connection.

Softwares prerequistive knowledge : RoR,MYSQL,Linux.

Platform                :  Browser based application works independant
on any platform

Client                  :  RoR application served on any compatible browser
Client Os               :  Symbian O/s or equivalent
Client hardware         :   Nokia 6600 or equivalent

Server                  : Mysql db support
Server Os               : Linux OS
Server Hardware         : Intel dual core/core 2 duo processor



Wednesday, January 12, 2011

Today I Learn class class, class object,class Integer in Ruby

Ruby is a perfect Object Oriented Programming Language. The features of the object-oriented programming language include:
  • Data Encapsulation:
  • Data Abstraction:
  • Polymorphism:
  • Inheritance:


class class :
         Class is also is an Object (like any other object in the system) but it is not direct instance of Object, it is an instance of a derived class (Class in this case)

Class object:
        An object-oriented program involves classes and objects. A class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
Class integer: