Rail3 intro 29th_sep_surendran

1,006
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,006
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rail3 intro 29th_sep_surendran

  1. 1. Rails3 – Introduction-I - Surendran S http://www.Spritle.com Copyright: Spritle Software Private Limited 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
  2. 2. About Me http://www.Spritle.com Copyright: Spritle Software Private Limited
  3. 4. <ul><ul><li>Rails is a web application development framework written in the Ruby language. </li></ul></ul><ul><ul><li>DRY – “Don’t Repeat Yourself” </li></ul></ul><ul><ul><li>REST is the best pattern for web applications </li></ul></ul>Rails Introduction 
  4. 5. <ul><ul><li>Rails is the Model, View, Controller architecture, usually just called MVC. </li></ul></ul>Rails Architecture 
  5. 6. <ul><ul><li>A model represents the information (data) of the application and the rules to manipulate that data.  </li></ul></ul><ul><ul><li>In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. </li></ul></ul><ul><ul><li>  In most cases, one table in your database will correspond to one model in your application.  </li></ul></ul><ul><ul><li>The bulk of your application’s business logic will be concentrated in the models   </li></ul></ul>MODEL
  6. 7. <ul><ul><li>Views represent the user interface of your application. </li></ul></ul><ul><ul><li>In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data.  </li></ul></ul><ul><ul><li>Views handle the job of providing data to the web browser or other tool that is used to make requests from your application. </li></ul></ul>VIEW
  7. 8. <ul><ul><li>Controllers provide the “glue” between models and views. </li></ul></ul><ul><ul><li>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. </li></ul></ul>CONTROLLER
  8. 9. <ul><ul><li>Rest stands for Representational State Transfer and is the foundation of the RESTful architecture. </li></ul></ul><ul><ul><li>  For example, to a Rails application a request such as this:DELETE /photos/17 </li></ul></ul>REST
  9. 10.   $rails new sample Creates a new rails project   CREATING NEW RAILS APP
  10. 11.  
  11. 12. <ul><li>  $rails new sample </li></ul><ul><li>Creates a new rails project </li></ul><ul><li>Configuring a Database </li></ul><ul><li>The database to use is specified in a configuration file, config/database.yml </li></ul><ul><li>he file contains sections for three different environments in which Rails can run by default: </li></ul><ul><ul><li>The development environment is used on your development computer as you interact manually with the application </li></ul></ul><ul><ul><li>The test environment is used to run automated tests </li></ul></ul><ul><ul><li>The production environment is used when you deploy your application for the world to use. </li></ul></ul><ul><li>  </li></ul><ul><li>The database to use is specified in a configuration file, config/database.yml </li></ul><ul><li>The file contains sections for three different environments in which Rails can run by default: </li></ul><ul><ul><li>The development environment is used on your development computer as you interact manually with the application </li></ul></ul><ul><ul><li>The test environment is used to run automated tests </li></ul></ul><ul><ul><li>The production environment is used when you deploy your application for the world to use. </li></ul></ul><ul><li>  </li></ul>CONFIGURING DATABASE
  12. 13. $ rake db:create $ rails server This will fire up an instance of the Mongrel web server by default  To see your application in action, open a browser window and navigate to http://localhost:3000 . You should see Rails’ default information page: STARING SERVER
  13. 14. <ul><ul><li>Brand new router  </li></ul></ul><ul><ul><li>New Active Record chainable query language built on top of relational algebra </li></ul></ul><ul><ul><li>Unobtrusive JavaScript helpers with drivers for Prototype, jQuery, and more coming (end of inline JS) </li></ul></ul><ul><ul><li>Action controller respond_with </li></ul></ul><ul><ul><li>Explicit dependency management with Bundler  </li></ul></ul>Whats new in rails3   
  14. 15.            Old                                             New script/generate                              rails g script/console                                rails c script/server                                  rails s script/dbconsole                            rails db
  15. 16. $ rails generate scaffold Post name:string title:string content:text
  16. 17.      Old                                                     New script/generate                              rails g script/console                                rails c script/server                                  rails s script/dbconsole                             rails db rake db:migrate creates a table and add the 3 columns in the table  MODEL AND MIGRATION
  17. 18. $ rails generate model Comment commenter:string body:text post:references
  18. 19. Rails 2 config/routes.rb TestApp::Application.routes.draw do |map| map.resources :posts end Rails 3 TestApp::Application.routes.draw do resources :posts end ROUTES
  19. 20. creates seven different routes in your application, all mapping to the Posts controller ROUTES
  20. 21. Rails 2 resources :posts do |post| post.resources :comments end Rails 3 resources :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
  21. 22. Rails 2 @posts = Post.find(:all, :conditions => {:published => true}) immediately queries the db returns an Array of Posts Rails 3@posts = Post.where(:published => true) doesn’t query the db returns an ActiveRecord::Relation ActiveRelation
  22. 23.      Old                                                     New script/generate                              rails g script/console                                rails c script/server                                  rails s script/dbconsole                             rails db @posts.each do |p| ... end ActiveRelation Query runs here this is Lazy loading
  23. 24. @published = Post.published will return  you the posts whose ppublished column is true Class Post < ActiveRecord::Base default_scope order('title') scope :published, where(:published => true) scope :unpublished, where(:published => false) end ActiveRelation
  24. 25. Rails 2 Post.find(:all, :conditions => {:author => &quot;Joe&quot;}, :includes => :comments,:order => &quot;title&quot;, :limit => 10) Rails 3 Post.where(:author=>Joe&quot;).include(:comments).order(:title).limit(10) ActiveRelation
  25. 26. BUNDLER $bundle install  Will ensure all gems are installed, including webrat (but it’s only included in test mode). $bundle --without test Will install everything except webrat. gemfile
  26. 27. HTML 5 custom data attributes data-* Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements data-remote data-method data-confirm data-disable-with Adopting Unobtrusive Javascript
  27. 28. Rails 2 <%= link_to_remote 'Show', :url => post %> <a href=&quot;#&quot; onclick=&quot;new Ajax.Request('/posts/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('9sk..44d')}); return false;&quot;>Show</a> Rails 3 <%= link_to 'Show', post, :remote => true %> <a href=&quot;/posts/1&quot; data-remote=&quot;true&quot;>Show</a> Adopting Unobtrusive Javascript
  28. 29.   Adopting Unobtrusive Javascript
  29. 30.   Adopting Unobtrusive Javascript document.observe(&quot;dom:loaded&quot;, function() { $(document.body).observe(&quot;click&quot;, function(event) { var message = event.element().readAttribute('data-confirm'); if (message) { // ... Do a confirm box } var element = event.findElement(&quot;a[data-remote=true]&quot;); if (element) { // ... Do the AJAX call } var element = event.findElement(&quot;a[data-method]&quot;); if (element) { // ... Create a form } })
  30. 31. http://github.com/rails/jquery-uj $('a[data-confirm],input[data-confirm]').live('click', function () { // ... Do a confirm box }); $('form[data-remote=&quot;true&quot;]').live('submit', function (e) { // ... Do an AJAX call }); jQuery in Rails?
  31. 32. Thanks & Questions

×