Rail3 intro 29th_sep_surendran

32
http://www.Spritle.com Copyright: Spritle Software Private Limited Rails3 – Introduction-I - Surendran S Please note that some of the images or content might have been taken from Internet and we don’t own copyright on them. If you have any objection in the content, please let us know at [email protected]

Transcript of Rail3 intro 29th_sep_surendran

Page 1: Rail3 intro 29th_sep_surendran

http://www.Spritle.comCopyright: Spritle Software Private Limited

Rails3 – Introduction-I-

Surendran S

Please note that some of the images or content might have been taken from Internet and we don’t own copyright on them. If you have any objection in the content, please let us know at [email protected]

Page 2: Rail3 intro 29th_sep_surendran

http://www.Spritle.comCopyright: Spritle Software Private Limited

About Me

Page 3: Rail3 intro 29th_sep_surendran
Page 4: Rail3 intro 29th_sep_surendran

• Rails is a web application development framework written in the Ruby language.

• DRY – “Don’t Repeat Yourself”

• REST is the best pattern for web applications

Rails Introduction 

Page 5: Rail3 intro 29th_sep_surendran

• Rails is the Model, View, Controller architecture, usually just called MVC.

Rails Architecture 

Page 6: Rail3 intro 29th_sep_surendran

• A model represents the information (data) of the application and the rules to manipulate that data. 

• In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table.

•  In most cases, one table in your database will correspond to one model in your application. 

• The bulk of your application’s business logic will be concentrated in the models 

MODEL

Page 7: Rail3 intro 29th_sep_surendran

• Views represent the user interface of your application.

• In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data. 

• Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.

VIEW

Page 8: Rail3 intro 29th_sep_surendran

• Controllers provide the “glue” between models and views.

• In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.

CONTROLLER

Page 9: Rail3 intro 29th_sep_surendran

• Rest stands for Representational State Transfer and is the foundation of the RESTful architecture.

•  For example, to a Rails application a request such as this:DELETE /photos/17

REST

Page 10: Rail3 intro 29th_sep_surendran

 $rails new sample

Creates a new rails project 

CREATING NEW RAILS APP

Page 11: Rail3 intro 29th_sep_surendran

 

Page 12: Rail3 intro 29th_sep_surendran

 $rails new sampleCreates a new rails projectConfiguring a DatabaseThe database to use is specified in a configuration file, config/database.ymlhe file contains sections for three different environments in which Rails can run by default: The development environment is used on your development

computer as you interact manually with the application The test environment is used to run automated tests The production environment is used when you deploy your

application for the world to use.

 

The database to use is specified in a configuration file, config/database.yml

The file contains sections for three different environments in which Rails can run by default: The development environment is used on your

development computer as you interact manually with the application

The test environment is used to run automated tests The production environment is used when you

deploy your application for the world to use.

 

CONFIGURING DATABASE

Page 13: Rail3 intro 29th_sep_surendran

$ rake db:create$ rails serverThis will fire up an instance of the Mongrel web server by default To see your application in action, open a browser window and navigate tohttp://localhost:3000. You should see Rails’ default information page:

STARING SERVER

Page 14: Rail3 intro 29th_sep_surendran

• Brand new router • New Active Record chainable query language built on top of

relational algebra• Unobtrusive JavaScript helpers with drivers for Prototype,

jQuery, and more coming (end of inline JS)• Action controller respond_with• Explicit dependency management with Bundler 

Whats new in rails3   

Page 15: Rail3 intro 29th_sep_surendran

   

      Old                                             Newscript/generate                              rails gscript/console                                rails cscript/server                                  rails sscript/dbconsole                            rails db

Page 16: Rail3 intro 29th_sep_surendran

$ rails generate scaffold Post name:string title:string content:text

Page 17: Rail3 intro 29th_sep_surendran

    Old                                                     Newscript/generate                              rails gscript/console                                rails cscript/server                                  rails sscript/dbconsole                             rails db rake db:migrate

creates a table and add the 3 columns in the table 

MODEL AND MIGRATION

Page 18: Rail3 intro 29th_sep_surendran

$ rails generate model Comment commenter:string body:text post:references

Page 19: Rail3 intro 29th_sep_surendran

Rails 2

config/routes.rb

TestApp::Application.routes.draw do |map|map.resources :postsend

Rails 3

TestApp::Application.routes.draw doresources :postsend

ROUTES

Page 20: Rail3 intro 29th_sep_surendran

creates seven different routes in your application, all mapping to the Posts controller

ROUTES

Page 21: Rail3 intro 29th_sep_surendran

Rails 2resources :posts do |post| post.resources :comments end

Rails 3resources :posts do resources :comments end

This creates comments as a nested resource within posts. This is another part of capturing the hierarchical relationship that exists between posts and comments

ROUTES

Page 22: Rail3 intro 29th_sep_surendran

Rails 2@posts = Post.find(:all, :conditions => {:published => true})immediately queries the dbreturns an Array of Posts

Rails 3@posts = Post.where(:published => true)doesn’t query the dbreturns an ActiveRecord::Relation

ActiveRelation

Page 23: Rail3 intro 29th_sep_surendran

    Old                                                     Newscript/generate                              rails gscript/console                                rails cscript/server                                  rails sscript/dbconsole                             rails db

@posts.each do |p|... end

ActiveRelation

Query runs here this is Lazy loading

Page 24: Rail3 intro 29th_sep_surendran

@published = Post.publishedwill return  you the posts whose ppublished column is true

Class Post < ActiveRecord::Basedefault_scope order('title')scope :published, where(:published => true)scope :unpublished, where(:published => false)end

ActiveRelation

Page 25: Rail3 intro 29th_sep_surendran

Rails 2

Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments,:order => "title", :limit => 10)

Rails 3Post.where(:author=>Joe").include(:comments).order(:title).limit(10)

ActiveRelation

Page 26: Rail3 intro 29th_sep_surendran

BUNDLER

$bundle install Will ensure all gems are installed, including webrat (but it’s only included in test mode).$bundle --without testWill install everything except webrat.

gemfile

Page 27: Rail3 intro 29th_sep_surendran

HTML 5 custom data attributesdata-*Custom data attributes are intended to store custom data private to the page orapplication, for which there are no more appropriate attributes or elementsdata-remotedata-methoddata-confirmdata-disable-with

Adopting Unobtrusive Javascript

Page 28: Rail3 intro 29th_sep_surendran

Rails 2<%= link_to_remote 'Show', :url => post %><a href="#" onclick="new Ajax.Request('/posts/1', {asynchronous:true,evalScripts:true, parameters:'authenticity_token=' +encodeURIComponent('9sk..44d')}); return false;">Show</a>

Rails 3<%= link_to 'Show', post, :remote => true %><a href="/posts/1" data-remote="true">Show</a>

Adopting Unobtrusive Javascript

Page 29: Rail3 intro 29th_sep_surendran

 

Adopting Unobtrusive Javascript

Page 30: Rail3 intro 29th_sep_surendran

 

Adopting Unobtrusive Javascriptdocument.observe("dom:loaded", function() {$(document.body).observe("click", function(event) {var message = event.element().readAttribute('data-confirm');if (message) {// ... Do a confirm box}var element = event.findElement("a[data-remote=true]");if (element) {// ... Do the AJAX call}var element = event.findElement("a[data-method]");if (element) {// ... Create a form}})

Page 31: Rail3 intro 29th_sep_surendran

http://github.com/rails/jquery-uj

$('a[data-confirm],input[data-confirm]').live('click', function () {// ... Do a confirm box});$('form[data-remote="true"]').live('submit', function (e) {// ... Do an AJAX call});

jQuery in Rails?

Page 32: Rail3 intro 29th_sep_surendran

Thanks &

Questions