What's new in Rails 2?
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

What's new in Rails 2?

on

  • 30,238 views

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

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

Statistics

Views

Total Views
30,238
Views on SlideShare
29,812
Embed Views
426

Actions

Likes
38
Downloads
838
Comments
3

15 Embeds 426

http://www.informatik-student.de 334
http://railsfs.blogspot.com 35
http://www.slideshare.net 22
http://cntt.tv 19
http://www.dmitvn.com 3
http://peleqe2.forogratis.es 3
http://www.filescon.com 2
http://blog.maxschulze.com 1
http://maxschulze.tumblr.com 1
file:// 1
http://www.rapidsharefast.com 1
http://www.sadavi.com.vn 1
http://funwithprogramming.blogspot.com 1
http://simplesideias.com.br 1
http://gmodules.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 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
    Your message goes here
    Processing…
  • 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
    Your message goes here
    Processing…
  • 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
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Can everyone hear me? 40% as good as PHP.

What's new in Rails 2? Presentation Transcript

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