SlideShare a Scribd company logo
1 of 63
Download to read offline
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

What's hot

RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
Bo-Yi Wu
 
Your first sinatra app
Your first sinatra appYour first sinatra app
Your first sinatra app
Rubyc Slides
 

What's hot (20)

Basic Git commands
Basic Git commandsBasic Git commands
Basic Git commands
 
Retrofit
RetrofitRetrofit
Retrofit
 
Rails on Rack
Rails on RackRails on Rack
Rails on Rack
 
Selenium&amp;scrapy
Selenium&amp;scrapySelenium&amp;scrapy
Selenium&amp;scrapy
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Presentation laravel 5 4
Presentation laravel 5 4Presentation laravel 5 4
Presentation laravel 5 4
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
 
Your first sinatra app
Your first sinatra appYour first sinatra app
Your first sinatra app
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
 
Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
CakePHP and AJAX
CakePHP and AJAXCakePHP and AJAX
CakePHP and AJAX
 
Web Development in Perl
Web Development in PerlWeb Development in Perl
Web Development in Perl
 
Perl in the Internet of Things
Perl in the Internet of ThingsPerl in the Internet of Things
Perl in the Internet of Things
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
 
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTP
 
Cloud Automation with Opscode Chef
Cloud Automation with Opscode ChefCloud Automation with Opscode Chef
Cloud Automation with Opscode Chef
 
Android Libs - Retrofit
Android Libs - RetrofitAndroid Libs - Retrofit
Android Libs - Retrofit
 
Rest api with Python
Rest api with PythonRest api with Python
Rest api with Python
 
Webscraping with asyncio
Webscraping with asyncioWebscraping with asyncio
Webscraping with asyncio
 
Catalyst MVC
Catalyst MVCCatalyst MVC
Catalyst MVC
 

Viewers also liked

Lucas Caton - Apresentação no encontro do Guru-SP #11: "Controlando estado de...
Lucas Caton - Apresentação no encontro do Guru-SP #11: "Controlando estado de...Lucas Caton - Apresentação no encontro do Guru-SP #11: "Controlando estado de...
Lucas Caton - Apresentação no encontro do Guru-SP #11: "Controlando estado de...
Lucas Caton
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slides
Cao Van An
 

Viewers also liked (10)

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

Similar to What's new in Rails 4

03 form-data
03 form-data03 form-data
03 form-data
snopteck
 

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

New PHP Exploitation Techniques
New PHP Exploitation TechniquesNew PHP Exploitation Techniques
New PHP Exploitation Techniques
 
Tdc 2013 - Ecossistema Ruby
Tdc 2013 - Ecossistema RubyTdc 2013 - Ecossistema Ruby
Tdc 2013 - Ecossistema Ruby
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
 
ZendCon 08 php 5.3
ZendCon 08 php 5.3ZendCon 08 php 5.3
ZendCon 08 php 5.3
 
Laravel intake 37 all days
Laravel intake 37 all daysLaravel intake 37 all days
Laravel intake 37 all days
 
CakePHP
CakePHPCakePHP
CakePHP
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Rails 101
Rails 101Rails 101
Rails 101
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
Workshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - phpbelfastWorkshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - phpbelfast
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
 
Installing php 7.4 Nginx Laravel 7.x on Centos 8
Installing php 7.4 Nginx Laravel 7.x on Centos 8Installing php 7.4 Nginx Laravel 7.x on Centos 8
Installing php 7.4 Nginx Laravel 7.x on Centos 8
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss
 
Apex REST
Apex RESTApex REST
Apex REST
 
03 form-data
03 form-data03 form-data
03 form-data
 
Getting started with DSpace 7 REST API
Getting started with DSpace 7 REST APIGetting started with DSpace 7 REST API
Getting started with DSpace 7 REST API
 
SharePoint 2013 REST APIs
SharePoint 2013 REST APIsSharePoint 2013 REST APIs
SharePoint 2013 REST APIs
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

What's new in Rails 4