What's new in Rails 2?


Published on

An overview of the major new features and changes in Rails 2.0. Originally presented on December 11th, 2007 at NYC.rb.

Published in: Business, Technology
  • I downloaded this ppt file and when I try to open via MS Powerpoint 2007, the PP opens but the file contents isn't shown at all.
    Does the downloaded file support MS 2007???
    Are you sure you want to  Yes  No
    Your message goes here
  • Unfortunately the slideshow importer crash some parts of presentation. I suggest read the slides in PDF form also.
    I'm a enthusiast at Rails' World and this new version seems like very great.
    I'll try test this new version soon in a project that I need develop.
    Thanks Bryan to excellent explanation about Rails 2.<br /><br/>
    Are you sure you want to  Yes  No
    Your message goes here
  • The SlideShow importer choked on my syntax-highlighted code samples. Check out the full thing (in PDF form) at http://www.brynary.com/2007/12/12/what-s-new-in-rails-2-slides-posted<br /><br/>
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Can everyone hear me? 40% as good as PHP.
  • What's new in Rails 2?

    1. What’s new in Rails 2? <ul><li>Bryan Helmkamp </li></ul><ul><li>http://brynary.com / </li></ul>
    2. “Look at all the things I’m NOT doing.” <ul><li>–DHH during the “Creating a blog </li></ul><ul><li>in 15 minutes” screencast </li></ul>
    3. Evolutionary, not revolutionary.
    4. ActiveRecord
    5. Numerical validation <ul><li>Gains the ability to specify comparisons. </li></ul><ul><li>Examples: </li></ul><ul><li>validates_numericality_of :salary , :greater_than => 49_000 </li></ul><ul><li>validates_numericality_of :age , :less_than_or_equal => 99 </li></ul><ul><li>validates_numericality_of :employees , :greater_than => 0 </li></ul>
    6. :allow_blank => true <ul><li>Skips validation for empty strings and nil. </li></ul>
    7. XML in, JSON out <ul><li>>> Post.new .from_xml ({:title => &quot;Hello!&quot;, :body => &quot;text&quot;}.to_xml) </li></ul><ul><li>=> #<Post id: nil, title: &quot;Hello!&quot;, body: &quot;text&quot;, created_at: nil, updated_at: nil> </li></ul><ul><li>>> Post.new(:title => &quot;Hello!&quot;, :body => &quot;text&quot;) .to_json </li></ul><ul><li>=> &quot;{&quot;updated_at&quot;: null, &quot;title&quot;: &quot;Hello!&quot;, &quot;body&quot;: &quot;text&quot;, &quot;created_at&quot;: null}&quot; </li></ul>
    8. Sexy migrations <ul><li>create_table :posts do | t | </li></ul><ul><li>t.belongs_to :blog , :null => false </li></ul><ul><li>t.string :title , :subtitle </li></ul><ul><li>t.text :body </li></ul><ul><li>t.timestamps </li></ul><ul><li>end </li></ul>
    9. Query cache <ul><li>Very dumb. Checks SQL string equality. </li></ul><ul><li>Cleared by any INSERT/UPDATE/DELETE. </li></ul><ul><li>Enabled per-action by default. </li></ul><ul><li>Can be skipped if necessary: </li></ul><ul><li>def self.uncached_newest </li></ul><ul><li>uncached do </li></ul><ul><li>find( :all , :order => :created_at ) </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    10. Foxy Fixtures
    11. Three main features <ul><li>Don’t specify the IDs for each record. </li></ul><ul><li>Reference associated fixtures by name. </li></ul><ul><li>Sets created_at and updated_at to Time.now automatically. </li></ul>
    12. Foxy Fixtures example <ul><li># blogs.yml </li></ul><ul><li>my_blog: </li></ul><ul><li>name: &quot;My awesome blog&quot; </li></ul><ul><li># posts.yml </li></ul><ul><li>first_post: </li></ul><ul><li>blog: my_blog </li></ul><ul><li>title: &quot;First post!&quot; </li></ul>
    13. fixtures :all <ul><li>Load all fixtures for all test cases. (Recommended!) </li></ul>
    14. ActionPack
    15. Cookie sessions <ul><li>Now the default session store. </li></ul><ul><li>Stored in clear text, secured with a hash. </li></ul><ul><li>Configure a secret in environment.rb: </li></ul><ul><li>Rails :: Initializer .run do | config | </li></ul><ul><li>config.action_controller.session = { </li></ul><ul><li>:session_key => '_rails_app_session', </li></ul><ul><li>:secret => 'e96a9......aef9111a' </li></ul><ul><li>} </li></ul><ul><li>end </li></ul>
    16. HTTP-only cookies <ul><li>Prevents access to cookies from JavaScript. </li></ul>
    17. CSRF protection <ul><li>Enabled by default in application.rb </li></ul><ul><li>Uses a token to verify requests are from the application, not a malicious site. </li></ul><ul><li>Built-in to form_tag and friends. </li></ul>
    18. Partial layouts <ul><li>Use with partials: </li></ul><ul><li><% render :partial => &quot;post&quot;, :layout => &quot;window&quot; %> </li></ul><ul><li>or with a block: </li></ul><ul><li><% render :layout => &quot;window&quot;, :locals => { :name => &quot;Recent&quot; } do %> </li></ul><ul><li><% for post in @recent_posts %> </li></ul><ul><li><h2> <%=h post.title %> </h2> </li></ul><ul><li><!-- ... --> </li></ul><ul><li><% end %> </li></ul><ul><li><% end %> </li></ul>
    19. Route namespaces <ul><li>map.namespace :admin do | admin | </li></ul><ul><li># Maps to Admin::PostsController </li></ul><ul><li>admin.resources :posts </li></ul><ul><li>end </li></ul><ul><li># Maps to PostsController </li></ul><ul><li>map.resources :posts </li></ul>
    20. Semicolon is dead. Long live forward slash! Long live forward slash!
    21. Association routing shortcuts <ul><li>map.resources :blogs , :has_many => [ :posts , :themes ], </li></ul><ul><li>:has_one => :owner </li></ul>
    22. HTTP authentication <ul><li>class PostsController < ApplicationController </li></ul><ul><li>USER_NAME , PASSWORD = &quot;bryan&quot;, &quot;secret&quot; </li></ul><ul><li>before_filter :authenticate , :except => [ :index ] </li></ul><ul><li>... </li></ul><ul><li>private </li></ul><ul><li>def authenticate </li></ul><ul><li>authenticate_or_request_with_http_basic do | user_name , password | </li></ul><ul><li>user_name == USER_NAME && password == PASSWORD </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    23. Exception handlers <ul><li>class ApplicationController < ActionController::Base </li></ul><ul><li>rescue_from User :: NotAuthorized , :with => :deny_access </li></ul><ul><li>rescue_from 'MyAppError::Base' do | exception | </li></ul><ul><li>render :xml => exception.to_xml, :status => 500 </li></ul><ul><li>end </li></ul><ul><li>protected </li></ul><ul><li>def deny_access </li></ul><ul><li># ... </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    24. Asset caching <ul><li>Concatenates file contents. </li></ul><ul><li>Only active in production mode. </li></ul><ul><li>Examples: </li></ul><ul><li><%= stylesheet_link_tag &quot;reset&quot;, &quot;main&quot;, :cache => true %> </li></ul><ul><li><%# Cache in public/javascripts/special.js %> </li></ul><ul><li><%= javascript_include_tag &quot;whiz&quot;, &quot;bang&quot;, :cache => &quot;special&quot; %> </li></ul>
    25. Asset servers <ul><li>Distributes request to four asset hosts. </li></ul><ul><li>Speeds up page load time considerably. </li></ul><ul><li>In production.rb: </li></ul><ul><li>ActionController :: Base .asset_host = &quot;asset%d.example.com&quot; </li></ul>
    26. AtomFeedHelper <ul><li># index.atom.builder: </li></ul><ul><li>atom_feed do | feed | </li></ul><ul><li>feed.title(&quot;Bryan's Bytes&quot;) </li></ul><ul><li>feed.updated(( @posts .first.created_at)) </li></ul><ul><li>for post in @posts </li></ul><ul><li>feed.entry(post) do | entry | </li></ul><ul><li>entry.title(post.title) </li></ul><ul><li>entry.content(post.body, :type => 'html') </li></ul><ul><li>entry.author do | author | </li></ul><ul><li>author.name(&quot;NYC.rb&quot;) </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    27. Multiview <ul><li>Template filename changes: </li></ul><ul><ul><li>filename .format.renderer </li></ul></ul><ul><li>Examples: </li></ul><ul><ul><li>show.csv.erb </li></ul></ul><ul><ul><li>edit.html.haml </li></ul></ul><ul><ul><li>index.atom.builder </li></ul></ul>
    28. simply_helpful <ul><li>Now in core... but you should’ve been using it already. </li></ul>
    29. dom_class and dom_id <ul><li>dom_class(@person) => &quot;person&quot; </li></ul><ul><li>dom_class(Person) => &quot;person&quot; </li></ul><ul><li>dom_class(@person, :edit) => &quot;edit_person&quot; </li></ul><ul><li>dom_id(@person) => &quot;person_1234&quot; </li></ul><ul><li>dom_id(Person.new) => &quot;new_person&quot; </li></ul>
    30. div_for/content_tag_for <ul><li><% div_for @person do %> </li></ul><ul><li><!-- ... --> </li></ul><ul><li><% end %> </li></ul><ul><li><!-- Becomes --> </li></ul><ul><li><div class=&quot;person&quot; id=&quot;person_42&quot;> </li></ul><ul><li><!-- ... --> </li></ul><ul><li></div> </li></ul>
    31. url_for for models <ul><li>url_for(@post) works like post_path(@post). </li></ul><ul><li>url_for(Post.new) works like new_post_path. </li></ul><ul><li>Works with link_to, redirect_to, etc. </li></ul>
    32. render :partial for AR <ul><li>render :partial => @post works like render :partial => &quot;post&quot;, :object => @post </li></ul><ul><li>render :partial => @posts works like render :partial => &quot;post&quot;, :collection => @posts </li></ul>
    33. form_for <ul><li>Determines form action based on the record. (e.g. post_path or new_post_path) </li></ul><ul><li>New records use :method => :post </li></ul><ul><li>Existing records use :method => :put </li></ul><ul><li>Automatically adds a HTML class and ID </li></ul>
    34. form_for example <ul><li><% form_for @person do | f | %> </li></ul><ul><li><%= f.text_field :name %> </li></ul><ul><li><% end %> </li></ul><ul><li><!-- Becomes --> </li></ul><ul><li><form action=&quot;/people&quot; method=&quot;post&quot; class=&quot;person&quot; id=&quot;new_person&quot;> </li></ul><ul><li><input type=&quot;text&quot; name=&quot;person[name]&quot; id=&quot;person_name&quot; /> </li></ul><ul><li></form> </li></ul>
    35. Extra Goodies
    36. Debugging
    37. Getting started <ul><li>sudo gem install -y ruby-debug </li></ul><ul><li>Call the method “debugger” in your code </li></ul><ul><li>script/server --debugger </li></ul>
    38. Debugger commands <ul><li>irb – Drop into an irb session </li></ul><ul><li>list – List the code surround the breakpoint </li></ul><ul><li>p/pp – Print results of an expression </li></ul><ul><li>up – Move one frame higher </li></ul><ul><li>cont – Resume execution </li></ul><ul><li>next/step – Execute one line at a time. </li></ul><ul><li>where – Display the current stack </li></ul>
    39. Live demo
    40. assert_emails <ul><li># Assert one email is sent during action </li></ul><ul><li>assert_emails 1 do </li></ul><ul><li>post :signup , :name => &quot;Bryan&quot; </li></ul><ul><li>end </li></ul><ul><li># Assert no emails are sent </li></ul><ul><li>post :signup , :name => &quot;&quot; </li></ul><ul><li>assert_no_emails </li></ul>
    41. Hash filtering <ul><li>Hash#except is the inverse of Hash#slice. </li></ul><ul><li>Example: </li></ul><ul><li>>> {:foo => 1, :bar => 2}.except(:bar, :baz) </li></ul><ul><li>=> {:foo=>1} </li></ul>
    42. Database Rake tasks <ul><li>rake db:create and db:create:all </li></ul><ul><li>rake db:drop and db:drop:all </li></ul><ul><li>rake db:reset </li></ul><ul><li>rake db:rollback (defaults to STEP=1) </li></ul><ul><li>rake db:version </li></ul>
    43. List routes with Rake <ul><li>$ rake routes </li></ul><ul><li>posts GET /posts {:controller=>&quot;posts&quot;, :action=>&quot;index&quot;} </li></ul><ul><li>formatted_posts GET /posts.:format {:controller=>&quot;posts&quot;, :action=>&quot;index&quot;} </li></ul><ul><li>POST /posts {:controller=>&quot;posts&quot;, :action=>&quot;create&quot;} </li></ul><ul><li>POST /posts.:format {:controller=>&quot;posts&quot;, :action=>&quot;create&quot;} </li></ul><ul><li>new_post GET /posts/new {:controller=>&quot;posts&quot;, :action=>&quot;new&quot;} </li></ul><ul><li>formatted_new_post GET /posts/new.:format {:controller=>&quot;posts&quot;, :action=>&quot;new&quot;} </li></ul><ul><li>edit_post GET /posts/:id/edit {:controller=>&quot;posts&quot;, :action=>&quot;edit&quot;} </li></ul><ul><li>formatted_edit_post GET /posts/:id/edit.:format {:controller=>&quot;posts&quot;, :action=>&quot;edit&quot;} </li></ul><ul><li>post GET /posts/:id {:controller=>&quot;posts&quot;, :action=>&quot;show&quot;} </li></ul><ul><li>formatted_post GET /posts/:id.:format {:controller=>&quot;posts&quot;, :action=>&quot;show&quot;} </li></ul><ul><li>PUT /posts/:id {:controller=>&quot;posts&quot;, :action=>&quot;update&quot;} </li></ul><ul><li>PUT /posts/:id.:format {:controller=>&quot;posts&quot;, :action=>&quot;update&quot;} </li></ul><ul><li>DELETE /posts/:id {:controller=>&quot;posts&quot;, :action=>&quot;destroy&quot;} </li></ul><ul><li>DELETE /posts/:id.:format {:controller=>&quot;posts&quot;, :action=>&quot;destroy&quot;} </li></ul><ul><li>/:controller/:action/:id </li></ul><ul><li>/:controller/:action/:id.:format </li></ul>
    44. Initializer hooks <ul><li>Rails load order: </li></ul><ul><li>config/preinitializer.rb </li></ul><ul><li>config/environment.rb </li></ul><ul><li>config/environments/RAILS_ENV.rb </li></ul><ul><li>config/initializers/*.rb </li></ul>
    45. Code annotations <ul><li>Finds FIXME, TODO and OPTIMIZE comments. </li></ul><ul><li>$ rake notes </li></ul><ul><li>app/models/post.rb: </li></ul><ul><li>* [ 2] [TODO] : Add support for themes </li></ul><ul><li>app/views/layouts/application.rhtml: </li></ul><ul><li>* [ 1] [FIXME] Will break with less than three posts </li></ul>
    46. Request profiler <ul><li>sudo gem install -y ruby-prof </li></ul><ul><li>Example: </li></ul><ul><li>$ cat login_session.rb </li></ul><ul><li>get_with_redirect '/' </li></ul><ul><li>post_with_redirect '/sessions', :password => 'NYC.rb!' </li></ul><ul><li>$ ./script/performance/request -n 10 login_session.rb </li></ul>
    47. Losing weight
    48. ActionWebService is out. Use ActiveResource for REST. Use ActiveResource for REST.
    49. acts_as_* behaviors <ul><li>script/plugin install http://svn.rubyonrails.org/rails/plugins/acts_as_list/ </li></ul><ul><li>script/plugin install http://svn.rubyonrails.org/rails/plugins/acts_as_nested_set/ </li></ul><ul><li>script/plugin install http://svn.rubyonrails.org/rails/plugins/acts_as_tree/ </li></ul>
    50. Controller variables <ul><li>Don’t use @params, @session, @flash, @request or @env. </li></ul><ul><li>Just drop the “@” and use the methods. </li></ul>
    51. ActiveRecord finders <ul><li>Post.find_all becomes Post.find(:all). </li></ul><ul><li>Post.find_first becomes Post.find(:first). </li></ul>
    52. Components <ul><li>Interesting feature, but too slow . </li></ul><ul><li>Try refactoring to partials and filters... </li></ul><ul><li>...or grab Sebastian Delmont’s plugin: </li></ul><ul><li>http://dev.notso.net/svn/rails/plugins/embedded_actions/trunk/ </li></ul>
    53. Renamed routes <ul><li>Nested resources now use the parent name as a prefix. </li></ul><ul><li>Example: </li></ul><ul><li>map.resources :blogs do | blog | </li></ul><ul><li>blog.resources :posts </li></ul><ul><li>end </li></ul><ul><li>post_path becomes blog_ post_path, etc. </li></ul>
    54. Pagination <ul><li>Switch to will_paginate (recommended). </li></ul><ul><li>http://errtheblog.com/posts/56-im-paginating-again </li></ul><ul><li>Or install the classic_pagination plugin: </li></ul><ul><li>script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination </li></ul>
    55. Database adapters <ul><li>Adapters except SQLite, MySQL and PostgreSQL were removed from Rails core. </li></ul><ul><li>sudo gem install activerecord- oracle -adapter </li></ul>
    56. Form helpers <ul><li>start_form_tag and end_form_tag are gone. </li></ul><ul><li>Switch to the form_tag block syntax. </li></ul><ul><li>Use form_for when possible. </li></ul>
    57. form_tag example <ul><li><%= start_form_tag posts_path %> </li></ul><ul><li><%= text_field :post , :title %> </li></ul><ul><li><%= end_form_tag %> </li></ul><ul><li>becomes </li></ul><ul><li><% form_tag posts_path do %> </li></ul><ul><li><%= text_field :post , :title %> </li></ul><ul><li><% end %> </li></ul>
    58. AJAX view helpers <ul><li>Autocomplete and in-place editing moved to plugins. </li></ul><ul><li>script/plugin install http://svn.rubyonrails.org/rails/plugins/auto_complete/ </li></ul><ul><li>script/plugin install http://svn.rubyonrails.org/rails/plugins/in_place_editing/ </li></ul>
    59. “Use the Source, Luke.”
    60. How to upgrade <ul><li>Upgrade to Rails 1.2.6. </li></ul><ul><li>Eliminate deprecation warnings. </li></ul><ul><li>Make sure all your tests pass. </li></ul><ul><li>Upgrade to Rails 2.0.1. </li></ul><ul><li>Run Mislav’s Rails 2 compatibility checker script. ( http://pastie.caboo.se/private/krcevozww61drdeza13e3a ) </li></ul><ul><li>Make sure all your tests pass. </li></ul>
    61. Questions? What’s new in Rails 2? Bryan Helmkamp http://brynary.com / / / / <ul><li>http://www.flickr.com/photos/kaufman/81616562/ </li></ul><ul><li>http://icanhascheezburger.com/2007/01/12/wait-ill-fix-it/ </li></ul>Photos used