Skip to content

Instantly share code, notes, and snippets.

@gkop gkop/notes.md

Created Jan 9, 2013
Embed
What would you like to do?
Noisebridge Ruby and Rails class 1/8/13: Rails 4 New Features (notes)

Rails 4 New Features

Noisebridge Ruby and Rails Class

2013-01-08

Gabe Kopley

gabe@railsschool.org

www.railsschool.org/l/rails-4-new-features


Major New Features

  • Cache digests

  • ActionController::Live

  • Strong parameters

  • Turbolinks


Creating an Edge Rails App

(tested with 1ce304f8165e4a930f1a8fb1f6706e61f6411096)

$ gem install bundler --pre
$ gem install i18n thor thread_safe
$ git clone git://github.com/rails/rails.git
$ rails/railties/bin/rails new <path> --edge
create  
create  README.rdoc
create  Rakefile
...

$ cd <path>
$ bundle exec rails --version
Rails 4.0.0.beta

(Note you must use bundle exec or binstubs because Rubygems doesn't know about gems bundled from git)


Feature: Cache digests


Cache digests


Cache digests, continued

Eg. user profile shows his posts and the comments for each post

Profiles, posts, and comments are all fragment cached

  • Expire comment fragment when comment template is changed

  • Expire post fragment when the post template or comment template is changed

  • Expire profile fragment when profile template, post template, or comment template is changed

  • (demo)


Feature: ActionController::Live


ActionController::Live

  • Stream data to client with utmost flexibility

  • Unlike other new features, this one is not packaged for use with Rails 3.2

  • Detailed post by Tenderlove

  • (demo)


Feature: Strong parameters


Strong parameters


Strong parameters, continued

Background


Strong parameters, continued, continued

More flexible than attr_accessible

(demo)

Strong Parameters screencast by RailsCasts Pro


Feature: Turbolinks


Turbolinks


Other new features


Support for PATCH

Now treats PATCH and PUT requests the same

When updating record, submits forms as "PATCH" requests with _method form parameter

(demo)


Native support for Postgres hstore

http://schneems.com/post/19298469372/you-got-nosql-in-my-postgres-using-hstore-in-rails


Majorly revised Getting Started Guide

http://edgeguides.rubyonrails.org/getting_started.html

No more scaffolding!

New approach: try something, it doesn't work, make it work, repeat...


Dependency on Ruby > 1.9.3

Gems you develop for Rails 4 can assume Ruby > 1.9.3


Lots of changes to ActiveRecord

See links on last slide for details

  • .all returns relation
  • #update_columns introduced
  • Relation#none introduced
  • Relation#not introduced
  • Relation#first and Relation#last order by id
  • Relation#order takes a hash of columns and their orders
  • Bang methods on relations
  • Whole bunch of deprecations (eg. many dynamic finders)
  • Models don't need to subclass ActiveRecord::Base, can mixin instead
  • ...

New form helpers

  • #collection_checkboxes
  • #collection_radiobuttons

HTML5-specific

  • #date_field

WelcomeController

No need to delete public/index.html anymore !


InfoController

http://localhost:3000/rails/info/routes


before_action loads models in scaffolded controllers

Slims down the controller action methods

(demo)


Better exceptions pages

  • Prettier

  • Routing errors show routes table

  • (demo)


Pure-Ruby template handler

app/views/foo/show.json.ruby

Useful for rendering data


Useful links


Thanks Noisebridge!

Noisebridge logo

If you enjoy the class, please consider making a donation to Noisebridge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.