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)