SlideShare a Scribd company logo
What's new in Rails 4
Lucas Caton
www.lucascaton.com.br
4
May 1, 2013
Rails 4.0: Release Candidate 1 released!
Ruby 1.8.7
Ruby 1.9.2
Ruby 1.9.3
Ruby 2.0.0
RubyGems 2.x
ThreadSafety
memcache-client
dalli
=> 20% faster
=> ThreadSafe
=> Easier to integrate with NewRelic RPM
ActiveRecord
ActiveModel
4
#where
Post.find_all_by_title('Rails 4')
Post.where(title: 'Rails 4')
#find_or_*
Post.find_or_initialize_by_title('Rails 4')
Post.find_or_initialize_by(title:'Rails4')
#update
@post.update_attributes(post_params)
@post.update(post_params)
#update_columns
@post.update_attribute(post_params)
@post.update_columns(post_params)
Skip validations!
#all
@posts = Post.scoped
@posts = Post.all
ActiveRecord::Relation
Scopes
scope :sold, where(state: 'sold')
default_scope where(state: 'available')
scope :sold, ->{ where(state: 'sold') }
default_scope ->{ where(state: 'available') }
class User < ActiveRecord::Base
def visible_posts
case role
when 'Country Manager'
Post.where(country: country)
when 'Reviewer'
Post.published
when 'Bad User'
[]
end
end
end
@posts = current_user.visible_posts
if @posts.any?
@posts.recent
else
[]
end
#none
class User < ActiveRecord::Base
def visible_posts
case role
when 'Country Manager'
Post.where(country: country)
when 'Reviewer'
Post.published
when 'Bad User'
Post.none
end
end
end
#not
if author
Post.where('author != ?', author)
else
Post.where('author IS NOT NULL')
end
Post.where.not(author: author)
#order
User.order('created_at DESC')
User.order(:name, 'created_at DESC')
User.order(created_at: :desc)
User.order(:name, created_at: :desc)
ActiveModel
4
ActiveModel
class SupportTicket
include ActiveModel::Conversion
include ActiveModel::Validations
extend ActiveModel::Naming
extend ActiveModel::Translation
attr_accessor :title, :description
validates_presence_of :title
validates_presence_of :description
end
ActiveModel
class SupportTicket
include ActiveModel::Model
attr_accessor :title, :description
validates_presence_of :title
validates_presence_of :description
end
Strong
Parameters
4
class User < ActiveRecord::Base
attr_accessible :name
end
# Controller
def update
if @user.update_attributes(params[:user])
redirect_to @user, notice: 'Updated'
end
end
Strong Parameters
class User < ActiveRecord::Base
end
# Controller
def update
user_params = params.require(:user).permit(:name)
if @user.update(params[:user])
redirect_to @user, notice: 'Updated'
end
end
Routes
4
match '/items/:id/purchase', to: 'items#purchase'
XSS Attack (Cross-site Scripting)
post '/items/:id/purchase', to: 'items#purchase'
match '/items/:id/purchase', to: 'items#purchase', via: :post
match '/items/:id/purchase', to: 'items#purchase', via: :all
PATCH Method for HTTP
Abstract
Several applications extending the Hypertext Transfer
Protocol (HTTP) require a feature to do partial resource
modification. The existing HTTP PUT method only allows
a complete replacement of a document. This proposal
adds a new HTTP method, PATCH, to modify an existing
HTTP resource.
http://tools.ietf.org/html/rfc5789
$ rake routes
items GET /items(.:format) items#index
POST /items(.:format) items#create
new_item GET /items/new(.:format) items#new
edit_item GET /items/:id/edit(.:format) items#edit
item GET /items/:id(.:format) items#show
PUT /items/:id(.:format) items#update
PATCH /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
PATCH HTTP verb
Action
Controller
4
#before_action
before_filter :set_user, only: [:update]
before_action :set_user, only: [:update]
Encrypted cookie stored
in the browser
Flash messages
<p id="notice"><%= flash[:notice] %></p>
<p id="notice"><%= notice %></p>
Streaming
Views
4
collections helpers
<% @owners.each do |owner| %>
<%= radio_button_tag :owner_id, owner.id %>
<%= owner.name %>
<% end %>
collection_radio_buttons(:item, :owner_id, @owners, :id, :name)
collection_check_boxes(:item, :owner_id, @owners, :id, :name)
date fields
<%= f.date_field :return_date %>
<input id="item_return_date" name="item[return_date]" type="date">
date fields
<%= f.date_field :return_date %>
TurboLinks
aka. full page pjax
Other improvements
=> Routing Concerns
=> Test Folder Structure
=> Caching improvements
=> Custom Flash Types
=> ActionController Live
=> ETags
=> jbuilder template handler
Links
Rails Guides - Upgrading from Rails 3.2 to Rails 4.0:
http://edgeguides.rubyonrails.org/
upgrading_ruby_on_rails.html#upgrading-from-rails-3-2-to-
rails-4-0
Rails Casts - Upgrading to Rails 4
http://railscasts.com/episodes/415-upgrading-to-rails-4
Code School - Learn Rails 4 best practices
http://www.codeschool.com/courses/rails-4-zombie-
outlaws
Thank you!
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4
What's new in Rails 4

More Related Content

PDF
Ruby HTTP clients
PDF
Ruby HTTP clients comparison
PPT
Learn REST API with Python
PPTX
DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python
PDF
Serverless Ballerina
KEY
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
KEY
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
PDF
8 Minutes On Rack
Ruby HTTP clients
Ruby HTTP clients comparison
Learn REST API with Python
DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python
Serverless Ballerina
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010
8 Minutes On Rack

What's hot (20)

PPTX
Basic Git commands
PDF
Retrofit
PDF
Rails on Rack
PDF
Selenium&amp;scrapy
PDF
Rack Middleware
ODP
Presentation laravel 5 4
PDF
RESTful API Design & Implementation with CodeIgniter PHP Framework
PDF
Your first sinatra app
PDF
Server Side Swift
PDF
Intro to Rack
PDF
CakePHP and AJAX
PPS
Web Development in Perl
PDF
Perl in the Internet of Things
PDF
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
PDF
Python tools for testing web services over HTTP
PDF
Cloud Automation with Opscode Chef
PDF
Android Libs - Retrofit
PDF
Rest api with Python
PDF
Webscraping with asyncio
PDF
Catalyst MVC
Basic Git commands
Retrofit
Rails on Rack
Selenium&amp;scrapy
Rack Middleware
Presentation laravel 5 4
RESTful API Design & Implementation with CodeIgniter PHP Framework
Your first sinatra app
Server Side Swift
Intro to Rack
CakePHP and AJAX
Web Development in Perl
Perl in the Internet of Things
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Python tools for testing web services over HTTP
Cloud Automation with Opscode Chef
Android Libs - Retrofit
Rest api with Python
Webscraping with asyncio
Catalyst MVC
Ad

Viewers also liked (10)

PDF
Expressões Regulares (Bio Labs #5)
PDF
Desenvolvimento ágil de software com Ruby on Rails
PPT
Lucas Caton - Apresentação no encontro do Guru-SP #11: "Controlando estado de...
PDF
Rails 3 ActiveRecord
PDF
Rails best practices_slides
PDF
named_scope more detail
PPTX
Active record(1)
PDF
Ruby on Rails 中級者を目指して - 大場寧子
PDF
ActiveRecord 2.3
PDF
Lean UX: Parem de criar produtos, comecem a criar experiências
Expressões Regulares (Bio Labs #5)
Desenvolvimento ágil de software com Ruby on Rails
Lucas Caton - Apresentação no encontro do Guru-SP #11: "Controlando estado de...
Rails 3 ActiveRecord
Rails best practices_slides
named_scope more detail
Active record(1)
Ruby on Rails 中級者を目指して - 大場寧子
ActiveRecord 2.3
Lean UX: Parem de criar produtos, comecem a criar experiências
Ad

Similar to What's new in Rails 4 (20)

PDF
Rails 4.0
PDF
What's new in ruby on rails 4
PDF
Rails 5 subjective overview
PDF
What’s New in Rails 5.0?
PPTX
Riding Rails 4
PPT
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
PDF
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
PPT
PPTX
Intro to Rails 4
PPTX
Learning to code for startup mvp session 3
PDF
Rails for Beginners - Le Wagon
PDF
Rails 2.0 Presentation
PDF
Introduction to Rails - presented by Arman Ortega
PPT
Ruby On Rails
KEY
Rails Presentation (Anton Dmitriyev)
ZIP
Barcamp Auckland Rails3 presentation
PDF
Ruby on Rails at PROMPT ISEL '11
KEY
Rails by example
ZIP
Rails 3 (beta) Roundup
ODP
Introduction to rails 4 v1
Rails 4.0
What's new in ruby on rails 4
Rails 5 subjective overview
What’s New in Rails 5.0?
Riding Rails 4
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Intro to Rails 4
Learning to code for startup mvp session 3
Rails for Beginners - Le Wagon
Rails 2.0 Presentation
Introduction to Rails - presented by Arman Ortega
Ruby On Rails
Rails Presentation (Anton Dmitriyev)
Barcamp Auckland Rails3 presentation
Ruby on Rails at PROMPT ISEL '11
Rails by example
Rails 3 (beta) Roundup
Introduction to rails 4 v1

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
Advanced methodologies resolving dimensionality complications for autism neur...
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf

What's new in Rails 4