SlideShare ist ein Scribd-Unternehmen logo
1 von 29
What's new in Rails 4?
●

Security

●

Speed

●

Strong Parameters

●

Turbolinks

●

Russian Dolls Caching and Cache Digests

●

ActionController::Live

●

Upgrade from rails 3.2 to rails 4
Security

One major feature of rails 4 is security. Rails 4 applications are more secure
than rails 3.x.x. There were some security issues in rails 3, to fix them
different versions are available for these issues. Security is implemented by
default to make rails 4 applications more secure than it was before.
Speed
Another feature of rails 4 is speed. Rails 4
required ruby 2.0, 1.9.3+. The reason behind
that the ruby 2.0 is few times faster than ruby
1.9.3 or later. Rails 4 encourage to use ruby 2.0
to enhance the performance of applications.
Strong Parameters
●

Rails 4 tackles the mass assignment problem with the
new Strong Parameters gem. A Rails 3 application
might have a create action similar to the following
example
Strong Parameters
1. class UsersController < ApplicationController
2. def create
3.

@user = User.create(params[:user])

4.

# ... check validity, redirect, etc.

5.

end

6. end
Strong Parameters
●

You can protect against unexpected input with
declarations in the model:

1. class User < ActiveRecord::Base
2.

# Only allow the following attributes to be

3.

# mass-assigned

4.

attr_accessible :name, :email

5. end
Strong Parameters
1. class UsersController < ApplicationController
2.

def create

3.

@user = User.create(user_params)

4.

# ... check validity, redirect, etc.

5.

end

6.

private

7.

def user_params

8.
9.

params.require(:user).permit(:name, :email)
end

10.end
Strong Parameters
●

●

As you can see, the params hash in your
controller is not a normal hash. It’s actually an
instance of ActionController::Parameters,
which exposes the require and permit
methods.
The require method ensures that the specified
key is available in the params hash, and
raises an ActionController::ParameterMissing
exception if the key doesn’t exist.
Turbolinks
●

●

A new feature in Rails 4 is Turbolinks, a GEM
designed to make app navigation faster in the
browser.
In browsers with pushState support, clicking a
link causes the Turbolinks plug-in to kick in. It
makes an Ajax request, updates the URL with
pushState (so your back button works) and
uses JavaScript to update the <title> and
<body> in the DOM. The speed gains come
from not having to download and re parse
JavaScript and CSS assets.
Turbolinks
Turbolinks gracefully degrade for browsers which do not
support pushState. In these situations, the page’s links behave
as normal, causing a full page refresh.
Being included by default in Rails 4 is Turbolinks, a JavaScript
plugin very similar to PJAX. PJAX (pushState+ AJAX) is a
jQuery plugin created by Chris Wanstrath, which allows you to
update a specific section of a page with an AJAX request
without having to do a full HTTP request. By using pushState,
PJAX updates the browser's current URL without reloading any
page resources such as JavaScript or Stylesheets..
Turbolinks
Turbolinks also uses pushState and does the
exact same thing as PJAX, except that it does
not require a custom partial response from the
server. Turbolinks will perform an HTTP Request
just as the browser would, but then replaces the
<title> and<body> currently loaded in the DOM
with the response from the server.
It is a more automatic solution, that removes the
need to manage which sections of your page will
be replaced.
Turbolinks
If a Turbolinks enabled web application is accessed from a
browser that doesn't support pushState, the web
application will degrade gracefully and do a complete
HTTP request. Browsers that support pushState and all
related APIs are:
Safari 6.0+
IE10
Chrome 5.0+
Firefox 4.0+
Caching
●

Rails 4 brings an overhauled caching strategy.
First, action and page caching, as you may
know it from previous versions of Rails, have
been removed and extracted to gems: action
and page, respectively.
Russian Dolls
The technique of nesting fragment caches to maximize
cache hits is known as Russian doll caching. By nesting
fragment caches, it ensures that caches can be reused
even when content changes. When a change occurs to
the top-most fragment cache, only that cache must be
expired.
Every nested cache of the parent can be reused, which
provides a significant performance increase. A change to
the most nested fragment cache would start a chain
reaction to expire all parent caches.
Russian Dolls
1. class Team < ActiveRecord::Base
2.

has_many :members

3. end
4.
5. class Member < ActiveRecord::Base
6.

belongs_to :team, :touch => true

7. end
Russian Dolls
1. <% cache @team do %>
2.

<h1><%= @team.name %></h1>

3. <ul class="members">
4.

<%= render @team.members %>

5. </ul>
6. <% end %>
Russian Dolls
●

And in app/views/members/_member.html.erb:

1. <% cache member do %>
2. <li class="member">
3.

<%= member.name %>

4.

<span class="bio">

5.
6.

<%= member.bio %>
</span>

7. </li>
8. <% end %>
Russian Dolls
if we had a team with two members, a total of of 3
fragment caches would be written:
views/members/1-20121220141922
views/members/2-20121220141922
views/teams/2-20121220141922
Russian Dolls
The above technique will work seamlessly until you
have to modify the template of one of the fragments.
Since the template is not taken into account in the
fragment cache key, any changes to the template will
not expire the cache.
This quickly progresses in prefixing the fragment
cache keys with a version, so that an expiration will
be forced. A change in a nested fragment version, will
result in all parent versions needing a version bump
also.
Russian Dolls
<!-- app/views/teams/show.html.erb -->
<% cache ["v1", @team] do%>
<h1>Team: <%= @team.name %></h1>
<%= render @team.members %>
<% end %>
<!-- app/views/members/_member.html.erb -->
<% cache ["v1", member] do %>
<div class='member'>
<span><%= member.name %></span>
<p><%= member.bio %></p>
</div>
<% end %>
Russian Dolls
The above version prefixing results in the
following fragment caches:
views/v1/members/1-20121220141922
views/v1/members/2-20121220141922
views/v1/teams/2-20121220141922
Cache Digests
If someone forgets to change the version number of a
template, and all its dependents, then the entire Russian doll
caching technique breaks down quickly. This happens easily,
as there is no visual reference of template dependencies.
For example, looking at the app/views/teams/show.html.erb
template, there is no indication that each member has its
own fragment cache.
Cache Digests
Rails 4 solves this problem with cache digests. A
call to #cache in your views will now suffix a
digest of the template and its dependencies. No
longer will you need to worry about fragment
cache dependencies and versioning!
Cache Digests
<!-- app/views/teams/show.html.erb -->
<% cache @team do%>
<h1>Team: <%= @team.name %></h1>
<%= render @team.members %>
<% end %>
<!-- app/views/members/_member.html.erb -->
<% cache member do %>
<div class='member'>
<span><%= member.name %></span>
<p><%= member.bio %></p>
</div>
<% end %>
Cache Digests
This results in the following fragment caches, now suffixed with an
MD5 of the template itself:
views/members/120121220141922/74865fcb3e2752a0928fa4f89b3e4426
views/members/220121220141922/74865fcb3e2752a0928fa4f89b3e4426
views/teams/220121220141922/4277f85c137009873c093088ef609e60
ActionController::Live
●

The new ActionController::Live module
provides the ability to stream data to clients.
Simply include the module into a controller to
enable your app to send arbitrary streamed
data. You’ll have to use a threaded server, like
thin and puma, in order to stream data; actions
from streaming controllers run in a separate
thread.
ActionController::Live
1. class MyController < ActionController::Base
2.

include ActionController::Live

3. def stream
4.

response.headers['Content-Type'] = 'text/event-stream'

5.

100.times {

6.

response.stream.write "hello worldn"

7.

sleep 1

8.

}

9.

response.stream.close

10.

#must close the stream

11. end
12.end
ActionController::Live
●

●

●

You must write any headers before you call
write or close on the response stream.
You have to call close on the response stream
when you’re finished writing data.
Ensure that your actions are thread-safe, as
they will run in a separate thread.
References

1.http://www.codeschool.com/courses/rails-4-zombie-outlaws
2.http://rails4.codeschool.com/videos
3.https://github.com/rails/strong_parameters
4.http://net.tutsplus.com/tutorials/ruby/digging-into-rails-4/
5.https://github.com/rails/cache_digests
6.https://github.com/rShetty/ActionController--Live
7.http://blog.remarkablelabs.com/2012/11/rails-4-countdown-to-2013
8.http://weblog.rubyonrails.org/2012/3/21/strong-parameters/

Weitere ähnliche Inhalte

Was ist angesagt?

Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and ServletsRaghu nath
 
From CakePHP to Laravel
From CakePHP to LaravelFrom CakePHP to Laravel
From CakePHP to LaravelJason McCreary
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API07.pallav
 
All Aboard for Laravel 5.1
All Aboard for Laravel 5.1All Aboard for Laravel 5.1
All Aboard for Laravel 5.1Jason McCreary
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jspAnkit Minocha
 
New Features of ASP.NET 4.0
New Features of ASP.NET 4.0New Features of ASP.NET 4.0
New Features of ASP.NET 4.0Buu Nguyen
 
Glassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionGlassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionDanairat Thanabodithammachari
 
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Adventures in Laravel 5 SunshinePHP 2016 TutorialAdventures in Laravel 5 SunshinePHP 2016 Tutorial
Adventures in Laravel 5 SunshinePHP 2016 TutorialJoe Ferguson
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
10 Laravel packages everyone should know
10 Laravel packages everyone should know10 Laravel packages everyone should know
10 Laravel packages everyone should knowPovilas Korop
 
Glassfish JEE Server Administration - The Enterprise Server
Glassfish JEE Server Administration - The Enterprise ServerGlassfish JEE Server Administration - The Enterprise Server
Glassfish JEE Server Administration - The Enterprise ServerDanairat Thanabodithammachari
 
Take Your XPages Development to the Next Level
Take Your XPages Development to the Next LevelTake Your XPages Development to the Next Level
Take Your XPages Development to the Next Levelbalassaitis
 

Was ist angesagt? (20)

Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
From CakePHP to Laravel
From CakePHP to LaravelFrom CakePHP to Laravel
From CakePHP to Laravel
 
Restful API's with ColdFusion
Restful API's with ColdFusionRestful API's with ColdFusion
Restful API's with ColdFusion
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
All Aboard for Laravel 5.1
All Aboard for Laravel 5.1All Aboard for Laravel 5.1
All Aboard for Laravel 5.1
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
JEE Programming - 07 EJB Programming
JEE Programming - 07 EJB ProgrammingJEE Programming - 07 EJB Programming
JEE Programming - 07 EJB Programming
 
Tutorial asp.net
Tutorial  asp.netTutorial  asp.net
Tutorial asp.net
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Why Laravel?
Why Laravel?Why Laravel?
Why Laravel?
 
New Features of ASP.NET 4.0
New Features of ASP.NET 4.0New Features of ASP.NET 4.0
New Features of ASP.NET 4.0
 
Glassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionGlassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE Introduction
 
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Adventures in Laravel 5 SunshinePHP 2016 TutorialAdventures in Laravel 5 SunshinePHP 2016 Tutorial
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
 
Hack the Future
Hack the FutureHack the Future
Hack the Future
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
10 Laravel packages everyone should know
10 Laravel packages everyone should know10 Laravel packages everyone should know
10 Laravel packages everyone should know
 
Glassfish JEE Server Administration - The Enterprise Server
Glassfish JEE Server Administration - The Enterprise ServerGlassfish JEE Server Administration - The Enterprise Server
Glassfish JEE Server Administration - The Enterprise Server
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Take Your XPages Development to the Next Level
Take Your XPages Development to the Next LevelTake Your XPages Development to the Next Level
Take Your XPages Development to the Next Level
 
Hidden Gems in ColdFusion 2016
Hidden Gems in ColdFusion 2016Hidden Gems in ColdFusion 2016
Hidden Gems in ColdFusion 2016
 

Andere mochten auch

Rails Concerns and Turbolinks
Rails Concerns and TurbolinksRails Concerns and Turbolinks
Rails Concerns and TurbolinksNascenia IT
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4shnikola
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Coursepeter_marklund
 

Andere mochten auch (8)

What's New in Rails 5
What's New in Rails 5What's New in Rails 5
What's New in Rails 5
 
Rails Concerns and Turbolinks
Rails Concerns and TurbolinksRails Concerns and Turbolinks
Rails Concerns and Turbolinks
 
Riding Rails 4
Riding Rails 4Riding Rails 4
Riding Rails 4
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
少しだけ先取りRails4.0
少しだけ先取りRails4.0少しだけ先取りRails4.0
少しだけ先取りRails4.0
 
turbolinks攻略
turbolinks攻略turbolinks攻略
turbolinks攻略
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 

Ähnlich wie Introduction to rails 4 v1

[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?Srijan Technologies
 
The War on ActionView with Russian Doll Caching
The War on ActionView with Russian Doll CachingThe War on ActionView with Russian Doll Caching
The War on ActionView with Russian Doll CachingPeter Giacomo Lombardo
 
Understanding asset pipeline plugin
Understanding asset pipeline pluginUnderstanding asset pipeline plugin
Understanding asset pipeline pluginRailsCarma
 
Enough with the javas cript already! de Nicholas Zakas
Enough with the javas cript already! de Nicholas ZakasEnough with the javas cript already! de Nicholas Zakas
Enough with the javas cript already! de Nicholas ZakasKubide
 
Enough with the JavaScript already!
Enough with the JavaScript already!Enough with the JavaScript already!
Enough with the JavaScript already!Nicholas Zakas
 
Ajax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsAjax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsRaghavan Mohan
 
SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3Ben Abdallah Helmi
 
DrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingDrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingAshok Modi
 
Make Drupal Run Fast - increase page load speed
Make Drupal Run Fast - increase page load speedMake Drupal Run Fast - increase page load speed
Make Drupal Run Fast - increase page load speedAndy Kucharski
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083Divyam Pateriya
 
Useful Rails Plugins
Useful Rails PluginsUseful Rails Plugins
Useful Rails Pluginsnavjeet
 
SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3Ben Abdallah Helmi
 
Plain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticalsPlain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticalsAngela Byron
 

Ähnlich wie Introduction to rails 4 v1 (20)

Create a new project in ROR
Create a new project in RORCreate a new project in ROR
Create a new project in ROR
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
 
Ror caching
Ror cachingRor caching
Ror caching
 
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
 
The War on ActionView with Russian Doll Caching
The War on ActionView with Russian Doll CachingThe War on ActionView with Russian Doll Caching
The War on ActionView with Russian Doll Caching
 
Understanding asset pipeline plugin
Understanding asset pipeline pluginUnderstanding asset pipeline plugin
Understanding asset pipeline plugin
 
Enough with the javas cript already! de Nicholas Zakas
Enough with the javas cript already! de Nicholas ZakasEnough with the javas cript already! de Nicholas Zakas
Enough with the javas cript already! de Nicholas Zakas
 
Enough with the JavaScript already!
Enough with the JavaScript already!Enough with the JavaScript already!
Enough with the JavaScript already!
 
Ajax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsAjax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorials
 
SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3SCWCD : Servlet web applications : CHAP 3
SCWCD : Servlet web applications : CHAP 3
 
Ruby on rails RAD
Ruby on rails RADRuby on rails RAD
Ruby on rails RAD
 
DrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingDrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizing
 
Make Drupal Run Fast - increase page load speed
Make Drupal Run Fast - increase page load speedMake Drupal Run Fast - increase page load speed
Make Drupal Run Fast - increase page load speed
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Useful Rails Plugins
Useful Rails PluginsUseful Rails Plugins
Useful Rails Plugins
 
SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3SCWCD : Servlet web applications : CHAP : 3
SCWCD : Servlet web applications : CHAP : 3
 
Plain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticalsPlain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticals
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
BPMS1
BPMS1BPMS1
BPMS1
 
BPMS1
BPMS1BPMS1
BPMS1
 

Kürzlich hochgeladen

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Kürzlich hochgeladen (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

Introduction to rails 4 v1

  • 1. What's new in Rails 4? ● Security ● Speed ● Strong Parameters ● Turbolinks ● Russian Dolls Caching and Cache Digests ● ActionController::Live ● Upgrade from rails 3.2 to rails 4
  • 2. Security One major feature of rails 4 is security. Rails 4 applications are more secure than rails 3.x.x. There were some security issues in rails 3, to fix them different versions are available for these issues. Security is implemented by default to make rails 4 applications more secure than it was before.
  • 3. Speed Another feature of rails 4 is speed. Rails 4 required ruby 2.0, 1.9.3+. The reason behind that the ruby 2.0 is few times faster than ruby 1.9.3 or later. Rails 4 encourage to use ruby 2.0 to enhance the performance of applications.
  • 4. Strong Parameters ● Rails 4 tackles the mass assignment problem with the new Strong Parameters gem. A Rails 3 application might have a create action similar to the following example
  • 5. Strong Parameters 1. class UsersController < ApplicationController 2. def create 3. @user = User.create(params[:user]) 4. # ... check validity, redirect, etc. 5. end 6. end
  • 6. Strong Parameters ● You can protect against unexpected input with declarations in the model: 1. class User < ActiveRecord::Base 2. # Only allow the following attributes to be 3. # mass-assigned 4. attr_accessible :name, :email 5. end
  • 7. Strong Parameters 1. class UsersController < ApplicationController 2. def create 3. @user = User.create(user_params) 4. # ... check validity, redirect, etc. 5. end 6. private 7. def user_params 8. 9. params.require(:user).permit(:name, :email) end 10.end
  • 8. Strong Parameters ● ● As you can see, the params hash in your controller is not a normal hash. It’s actually an instance of ActionController::Parameters, which exposes the require and permit methods. The require method ensures that the specified key is available in the params hash, and raises an ActionController::ParameterMissing exception if the key doesn’t exist.
  • 9. Turbolinks ● ● A new feature in Rails 4 is Turbolinks, a GEM designed to make app navigation faster in the browser. In browsers with pushState support, clicking a link causes the Turbolinks plug-in to kick in. It makes an Ajax request, updates the URL with pushState (so your back button works) and uses JavaScript to update the <title> and <body> in the DOM. The speed gains come from not having to download and re parse JavaScript and CSS assets.
  • 10. Turbolinks Turbolinks gracefully degrade for browsers which do not support pushState. In these situations, the page’s links behave as normal, causing a full page refresh. Being included by default in Rails 4 is Turbolinks, a JavaScript plugin very similar to PJAX. PJAX (pushState+ AJAX) is a jQuery plugin created by Chris Wanstrath, which allows you to update a specific section of a page with an AJAX request without having to do a full HTTP request. By using pushState, PJAX updates the browser's current URL without reloading any page resources such as JavaScript or Stylesheets..
  • 11. Turbolinks Turbolinks also uses pushState and does the exact same thing as PJAX, except that it does not require a custom partial response from the server. Turbolinks will perform an HTTP Request just as the browser would, but then replaces the <title> and<body> currently loaded in the DOM with the response from the server. It is a more automatic solution, that removes the need to manage which sections of your page will be replaced.
  • 12. Turbolinks If a Turbolinks enabled web application is accessed from a browser that doesn't support pushState, the web application will degrade gracefully and do a complete HTTP request. Browsers that support pushState and all related APIs are: Safari 6.0+ IE10 Chrome 5.0+ Firefox 4.0+
  • 13. Caching ● Rails 4 brings an overhauled caching strategy. First, action and page caching, as you may know it from previous versions of Rails, have been removed and extracted to gems: action and page, respectively.
  • 14. Russian Dolls The technique of nesting fragment caches to maximize cache hits is known as Russian doll caching. By nesting fragment caches, it ensures that caches can be reused even when content changes. When a change occurs to the top-most fragment cache, only that cache must be expired. Every nested cache of the parent can be reused, which provides a significant performance increase. A change to the most nested fragment cache would start a chain reaction to expire all parent caches.
  • 15. Russian Dolls 1. class Team < ActiveRecord::Base 2. has_many :members 3. end 4. 5. class Member < ActiveRecord::Base 6. belongs_to :team, :touch => true 7. end
  • 16. Russian Dolls 1. <% cache @team do %> 2. <h1><%= @team.name %></h1> 3. <ul class="members"> 4. <%= render @team.members %> 5. </ul> 6. <% end %>
  • 17. Russian Dolls ● And in app/views/members/_member.html.erb: 1. <% cache member do %> 2. <li class="member"> 3. <%= member.name %> 4. <span class="bio"> 5. 6. <%= member.bio %> </span> 7. </li> 8. <% end %>
  • 18. Russian Dolls if we had a team with two members, a total of of 3 fragment caches would be written: views/members/1-20121220141922 views/members/2-20121220141922 views/teams/2-20121220141922
  • 19. Russian Dolls The above technique will work seamlessly until you have to modify the template of one of the fragments. Since the template is not taken into account in the fragment cache key, any changes to the template will not expire the cache. This quickly progresses in prefixing the fragment cache keys with a version, so that an expiration will be forced. A change in a nested fragment version, will result in all parent versions needing a version bump also.
  • 20. Russian Dolls <!-- app/views/teams/show.html.erb --> <% cache ["v1", @team] do%> <h1>Team: <%= @team.name %></h1> <%= render @team.members %> <% end %> <!-- app/views/members/_member.html.erb --> <% cache ["v1", member] do %> <div class='member'> <span><%= member.name %></span> <p><%= member.bio %></p> </div> <% end %>
  • 21. Russian Dolls The above version prefixing results in the following fragment caches: views/v1/members/1-20121220141922 views/v1/members/2-20121220141922 views/v1/teams/2-20121220141922
  • 22. Cache Digests If someone forgets to change the version number of a template, and all its dependents, then the entire Russian doll caching technique breaks down quickly. This happens easily, as there is no visual reference of template dependencies. For example, looking at the app/views/teams/show.html.erb template, there is no indication that each member has its own fragment cache.
  • 23. Cache Digests Rails 4 solves this problem with cache digests. A call to #cache in your views will now suffix a digest of the template and its dependencies. No longer will you need to worry about fragment cache dependencies and versioning!
  • 24. Cache Digests <!-- app/views/teams/show.html.erb --> <% cache @team do%> <h1>Team: <%= @team.name %></h1> <%= render @team.members %> <% end %> <!-- app/views/members/_member.html.erb --> <% cache member do %> <div class='member'> <span><%= member.name %></span> <p><%= member.bio %></p> </div> <% end %>
  • 25. Cache Digests This results in the following fragment caches, now suffixed with an MD5 of the template itself: views/members/120121220141922/74865fcb3e2752a0928fa4f89b3e4426 views/members/220121220141922/74865fcb3e2752a0928fa4f89b3e4426 views/teams/220121220141922/4277f85c137009873c093088ef609e60
  • 26. ActionController::Live ● The new ActionController::Live module provides the ability to stream data to clients. Simply include the module into a controller to enable your app to send arbitrary streamed data. You’ll have to use a threaded server, like thin and puma, in order to stream data; actions from streaming controllers run in a separate thread.
  • 27. ActionController::Live 1. class MyController < ActionController::Base 2. include ActionController::Live 3. def stream 4. response.headers['Content-Type'] = 'text/event-stream' 5. 100.times { 6. response.stream.write "hello worldn" 7. sleep 1 8. } 9. response.stream.close 10. #must close the stream 11. end 12.end
  • 28. ActionController::Live ● ● ● You must write any headers before you call write or close on the response stream. You have to call close on the response stream when you’re finished writing data. Ensure that your actions are thread-safe, as they will run in a separate thread.