Rail3 intro 29th_sep_surendran

Post on 10-May-2015

947 views 0 download

Tags:

Transcript of 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 info@spritle.com

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

About Me

• 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 

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

Rails Architecture 

• 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

• 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

• 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

• 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

 $rails new sample

Creates a new rails project 

CREATING NEW RAILS APP

 

 $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

$ 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

• 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   

   

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

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

    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

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

Rails 2

config/routes.rb

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

Rails 3

TestApp::Application.routes.draw doresources :postsend

ROUTES

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

ROUTES

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

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

    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

@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

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

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

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

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

 

Adopting Unobtrusive Javascript

 

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}})

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?

Thanks &

Questions