SlideShare a Scribd company logo
ActionPack – From request to response 
1 / 17
What is ActionPack? 
ActionPack is a: 
1. RoR module or component; 
2. gem actionpack 4.1.6 
3. framework for handling and responding to web requests (the V&C layers 
in MVC paradigm) 
2 / 17
It consists of: 
2 major components: 
1. Action Controller – performing the logic 
2. Action View – rendering a template # this will be extracted into a new 
gem from version 4.1. 
Now, it only has this big Action Controller, which does all the staf to handle 
requests and responses for every rails controller. 
3 / 17
What does ActionController? 
Action Controller takes the request passed by router and does the 
groundwork for producing the response. 
Also uses a set of smart conventions to handle the request (invisible for 
developer) – RESTful, naming conv., folders, extensions etc. 
Fetch or save data from a model and use a view (Action View) to create 
the output. 
# app/controollers/application_controller.rb 
class ApplicationController < ActionController::Base 
# Prevent CSRF attacks by raising an exception. 
# For APIs, you may want to use :null_session instead. 
protect_from_forgery with: :exception 
end 
4 / 17
What does ActionView? 
renders a template, using embedded ruby mingled with HTML or HAML. 
provides a set of helpers classes for handling common behavior for forms, 
dates and strings. 
Some features of Action View are tied to Active Record, but that 
doesn't mean Action View depends on Active Record. Action View is 
an independent package that can be used with any sort of Ruby 
libraries. -- Rails Guides 
Sample view using en ERB template: 
<h1>Names of all ruby developers</h1> 
<% @developers.each do |person| %> 
Name: <%= person.name %><br> 
<% end %> 
5 / 17
I. Action Controller 
1. Conventions 
2. Parameters 
3. Filters 
4. Flashes 
5. Variants 
6 / 17
I.1 Conventions 
Controller naming conventions: 
camelcase, always ending in ...Controller, 
plural over/versus singular form of the last but one word. 
MyApp::Application.routes.draw do 
resources :developers # mapped to DevelopersController 
resource :profile # ! ProfilesController 
end 
MyApp::Application.routes.draw do 
resource :profile, controller: 'profile' # override the default plural convention 
end 
Methods and actions: 
snakecase, routing action name = controller method name 
public methods are most of the time actions 
move to private visibility all auxiliary methods or filters 
7 / 17
I.2 Parameters 
GET, POST, PATCH, PUT parameters go into a new instance of 
ActionController::Parameters, and this instance is provided by params 
helper method 
Hash and Array Parameters 
routing parameters 
get '/developers/:status' => 'developers#index', foo: 'bar' 
# ... 
URL /developers/active 
# ... 
puts params[:status] # => active 
default_url_options 
class ApplicationController < ActionController::Base 
def default_url_options 
{ locale: I18n.locale } 
end 
end 
8 / 17
I.2 Parameters(2) 
Strong parameters 
def create 
Person.create(person_params) 
end 
private 
def person_params 
params.require(:person).permit(:name, :age) 
end 
you can also use permit on nested parameters 
params.require(:person).permit(:name, { emails: [] }, 
friends: [ :name, 
{ family: [ :name ], hobbies: [] }]) 
9 / 17
I.3 Filters 
are methods that are run before, after or "around" a controller action. 
are inherited, so if you set a filter on ApplicationController, it will be run 
on every controller in your application. 
Example: 
class ApplicationController < ActionController::Base 
before_action :require_login 
private 
def require_login 
unless logged_in? 
flash[:error] = "You must be logged in to access this section" 
redirect_to new_login_url # halts request cycle 
end 
end 
end 
10 / 17
I.3 Filters(2) 
More examples: 
skip_[before|after|around]_action, only: or except: 
class LoginsController < ApplicationController 
skip_before_action :require_login, only: [:new, :create] 
end 
? follow Open Closed Principle 
around_action - a before&after filter !? 
class ChangesController < ApplicationController 
around_action :wrap_in_transaction, only: :show 
private 
def wrap_in_transaction 
ActiveRecord::Base.transaction do 
begin 
yield 
ensure 
raise ActiveRecord::Rollback 
end 
end 11 / 17
I.4 Flashes 
The flash is a special part of the session which is cleared with each request. It 
is useful for passing messages (error messsages) form last request. There are 
two predefined flash keys :notice and :alert, but you can use any other 
custom keys. 
Usage examples: 
class LoginsController < ApplicationController 
def destroy 
session[:current_user_id] = nil 
flash[:notice] = "You have successfully logged out." 
redirect_to root_url 
end 
end 
or assign a flash message as part of the redirection: 
redirect_to root_url, notice: "You have successfully logged out." 
redirect_to root_url, alert: "You're stuck here!" 
redirect_to root_url, flash: { referral_code: 1234 } 
12 / 17
I.4 Flashes(2) - useful methods 
Flashes are available on session only for the next request. But you can alter 
this default behavior in two ways: 
1. using flash.keep method 
class MainController < ApplicationController 
# Let's say this action corresponds to root_url, but you want 
# all requests here to be redirected to UsersController#index. 
# If an action sets the flash and redirects here, the values 
# would normally be lost when another redirect happens, but you 
# can use 'keep' to make it persist for another request. 
def index 
# Will persist all flash values. 
flash.keep 
# You can also use a key to keep only some kind of value. 
# flash.keep(:notice) 
redirect_to users_url 
end 
end 
13 / 17
I.4 Flashes(3) - useful methods 
1. using flash.now mehtod 
class ClientsController < ApplicationController 
def create 
@client = Client.new(params[:client]) 
if @client.save 
# ... 
else 
flash.now[:error] = "Could not save client" 
render action: "new" 
end 
end 
end 
rails 4.1 FlashHash now behaves like a HashWithIndifferentAccess. 
flash[:notice] # => 'Successful login.' 
flash['notice'] # => 'Successful login.' 
14 / 17
I.5 ActionPack Variants (rails 4.1) 
We often want to render different html/json/xml templates for phones, tablets, 
and desktop browsers. Variants make it easy. 
The request variant is a specialization of the request format, like :tablet, 
:phone, or :desktop. 
It can be set using a before_action : 
def set_variants 
request.variant = :tablet if request.user_agent =~ /iPad/ 
.... 
end 
Respond to variants in the action just like you respond to formats: 
respond_to do |format| 
format.html do |html| 
html # renders app/views/projects/show.html.erb 
html.tablet # renders app/views/projects/show.html+tablet.erb 
html.phone { extra_setup } # renders app/views/projects/show.html+phone.erb 
end 
end 
15 / 17
I.5 ActionPack Variants(2) 
Variants also support the common any/all block that formats have. 
It works for both inline: 
respond_to do |format| 
format.html.any { render text: "any" } 
format.html.phone { render text: "phone" } 
end 
and block syntax: 
respond_to do |format| 
format.html do |variant| 
variant.any(:tablet, :phablet) { render text: "ablet html template" } 
variant.phone { render text: "phone" } 
end 
end 
16 / 17
Resources 
Rails Guides 
Rails API doc 
Action Pack 4.1 changelog 
17 / 17

More Related Content

What's hot

Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
Viral Solani
 
Servlet
ServletServlet
Servlet
Rami Nayan
 
JSP
JSPJSP
Java Custom Annotations- Part1
Java Custom Annotations- Part1Java Custom Annotations- Part1
Java Custom Annotations- Part1
Mohammad Sabir Khan
 
Spring REST Request Validation
Spring REST Request ValidationSpring REST Request Validation
Spring REST Request Validation
Mohammad Sabir Khan
 
Introduction To Mvc
Introduction To MvcIntroduction To Mvc
Introduction To Mvc
Volkan Uzun
 
Salesforce connector Example
Salesforce connector ExampleSalesforce connector Example
Salesforce connector Example
prudhvivreddy
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
Piyush Aggarwal
 
Automation framework using selenium webdriver with java
Automation framework using selenium webdriver with javaAutomation framework using selenium webdriver with java
Automation framework using selenium webdriver with java
Narayanan Palani
 
Appletjava
AppletjavaAppletjava
Appletjava
DEEPIKA T
 
Repository design pattern in laravel
Repository design pattern in laravelRepository design pattern in laravel
Repository design pattern in laravel
Sameer Poudel
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Rami Nayan
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
Lhouceine OUHAMZA
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
Brady Cheng
 
Automated ui testing with selenium. drupal con london 2011
Automated ui testing with selenium. drupal con london 2011Automated ui testing with selenium. drupal con london 2011
Automated ui testing with selenium. drupal con london 2011
Yuriy Gerasimov
 
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
Raja Rozali Raja Hasan
 
Building custom APIs
Building custom APIsBuilding custom APIs
Building custom APIs
Pierre MARTIN
 
Migrating from JSF1 to JSF2
Migrating from JSF1 to JSF2Migrating from JSF1 to JSF2
Migrating from JSF1 to JSF2
tahirraza
 
Ember Reusable Components and Widgets
Ember Reusable Components and WidgetsEmber Reusable Components and Widgets
Ember Reusable Components and Widgets
Sergey Bolshchikov
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
Kamal S
 

What's hot (20)

Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)Introduction to Laravel Framework (5.2)
Introduction to Laravel Framework (5.2)
 
Servlet
ServletServlet
Servlet
 
JSP
JSPJSP
JSP
 
Java Custom Annotations- Part1
Java Custom Annotations- Part1Java Custom Annotations- Part1
Java Custom Annotations- Part1
 
Spring REST Request Validation
Spring REST Request ValidationSpring REST Request Validation
Spring REST Request Validation
 
Introduction To Mvc
Introduction To MvcIntroduction To Mvc
Introduction To Mvc
 
Salesforce connector Example
Salesforce connector ExampleSalesforce connector Example
Salesforce connector Example
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
 
Automation framework using selenium webdriver with java
Automation framework using selenium webdriver with javaAutomation framework using selenium webdriver with java
Automation framework using selenium webdriver with java
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Repository design pattern in laravel
Repository design pattern in laravelRepository design pattern in laravel
Repository design pattern in laravel
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
 
Automated ui testing with selenium. drupal con london 2011
Automated ui testing with selenium. drupal con london 2011Automated ui testing with selenium. drupal con london 2011
Automated ui testing with selenium. drupal con london 2011
 
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
 
Building custom APIs
Building custom APIsBuilding custom APIs
Building custom APIs
 
Migrating from JSF1 to JSF2
Migrating from JSF1 to JSF2Migrating from JSF1 to JSF2
Migrating from JSF1 to JSF2
 
Ember Reusable Components and Widgets
Ember Reusable Components and WidgetsEmber Reusable Components and Widgets
Ember Reusable Components and Widgets
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 

Similar to Rupicon 2014 Action pack

2007 Zend Con Mvc
2007 Zend Con Mvc2007 Zend Con Mvc
2007 Zend Con Mvc
Pablo Morales
 
MidwestPHP 2016 - Adventures in Laravel 5
MidwestPHP 2016 - Adventures in Laravel 5 MidwestPHP 2016 - Adventures in Laravel 5
MidwestPHP 2016 - Adventures in Laravel 5
Joe Ferguson
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
Rails introduction
Rails introductionRails introduction
Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 Ajax
Wen-Tien Chang
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
fiyuer
 
2007 Zend Con Mvc Edited Irmantas
2007 Zend Con Mvc Edited Irmantas2007 Zend Con Mvc Edited Irmantas
2007 Zend Con Mvc Edited Irmantas
Irmantas Šiupšinskas
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slides
Cao Van An
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
Jim Jeffers
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
Brian Sam-Bodden
 
Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2 Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2
3camp
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
RORLAB
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
Michał Orman
 
Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
William Leeper
 
Symfony2 Introduction Presentation
Symfony2 Introduction PresentationSymfony2 Introduction Presentation
Symfony2 Introduction Presentation
Nerd Tzanetopoulos
 
Trustparency web doc spring 2.5 & hibernate
Trustparency web doc   spring 2.5 & hibernateTrustparency web doc   spring 2.5 & hibernate
Trustparency web doc spring 2.5 & hibernate
trustparency
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
Shabir Ahmad
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
Chul Ju Hong
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
Chul Ju Hong
 
ElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev EditionElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev Edition
Brett Profitt
 

Similar to Rupicon 2014 Action pack (20)

2007 Zend Con Mvc
2007 Zend Con Mvc2007 Zend Con Mvc
2007 Zend Con Mvc
 
MidwestPHP 2016 - Adventures in Laravel 5
MidwestPHP 2016 - Adventures in Laravel 5 MidwestPHP 2016 - Adventures in Laravel 5
MidwestPHP 2016 - Adventures in Laravel 5
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Rails introduction
Rails introductionRails introduction
Rails introduction
 
Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 Ajax
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
2007 Zend Con Mvc Edited Irmantas
2007 Zend Con Mvc Edited Irmantas2007 Zend Con Mvc Edited Irmantas
2007 Zend Con Mvc Edited Irmantas
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slides
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
 
Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2 Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
 
Symfony2 Introduction Presentation
Symfony2 Introduction PresentationSymfony2 Introduction Presentation
Symfony2 Introduction Presentation
 
Trustparency web doc spring 2.5 & hibernate
Trustparency web doc   spring 2.5 & hibernateTrustparency web doc   spring 2.5 & hibernate
Trustparency web doc spring 2.5 & hibernate
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
 
ElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev EditionElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev Edition
 

More from rupicon

RSpec matchers
RSpec matchersRSpec matchers
RSpec matchers
rupicon
 
DIY Cartography
DIY CartographyDIY Cartography
DIY Cartography
rupicon
 
Dr. PostGIS or: How I Learned to Stop Worrying and Love the Docs
Dr. PostGIS or: How I Learned to Stop Worrying and Love the DocsDr. PostGIS or: How I Learned to Stop Worrying and Love the Docs
Dr. PostGIS or: How I Learned to Stop Worrying and Love the Docs
rupicon
 
Are you tougher than a boy/girl scout?
Are you tougher than a boy/girl scout?Are you tougher than a boy/girl scout?
Are you tougher than a boy/girl scout?
rupicon
 
Johnny Cache
Johnny CacheJohnny Cache
Johnny Cache
rupicon
 
U wont bleev wut dis code doez
U wont bleev wut dis code doezU wont bleev wut dis code doez
U wont bleev wut dis code doez
rupicon
 
Rupicon 2014 Single table inheritance
Rupicon 2014 Single table inheritanceRupicon 2014 Single table inheritance
Rupicon 2014 Single table inheritance
rupicon
 
Rupicon 2014 useful design patterns in rails
Rupicon 2014 useful design patterns in railsRupicon 2014 useful design patterns in rails
Rupicon 2014 useful design patterns in rails
rupicon
 
Rupicon 2014 solid
Rupicon 2014 solidRupicon 2014 solid
Rupicon 2014 solid
rupicon
 
Rupicon 2014 caching
Rupicon 2014 cachingRupicon 2014 caching
Rupicon 2014 caching
rupicon
 

More from rupicon (10)

RSpec matchers
RSpec matchersRSpec matchers
RSpec matchers
 
DIY Cartography
DIY CartographyDIY Cartography
DIY Cartography
 
Dr. PostGIS or: How I Learned to Stop Worrying and Love the Docs
Dr. PostGIS or: How I Learned to Stop Worrying and Love the DocsDr. PostGIS or: How I Learned to Stop Worrying and Love the Docs
Dr. PostGIS or: How I Learned to Stop Worrying and Love the Docs
 
Are you tougher than a boy/girl scout?
Are you tougher than a boy/girl scout?Are you tougher than a boy/girl scout?
Are you tougher than a boy/girl scout?
 
Johnny Cache
Johnny CacheJohnny Cache
Johnny Cache
 
U wont bleev wut dis code doez
U wont bleev wut dis code doezU wont bleev wut dis code doez
U wont bleev wut dis code doez
 
Rupicon 2014 Single table inheritance
Rupicon 2014 Single table inheritanceRupicon 2014 Single table inheritance
Rupicon 2014 Single table inheritance
 
Rupicon 2014 useful design patterns in rails
Rupicon 2014 useful design patterns in railsRupicon 2014 useful design patterns in rails
Rupicon 2014 useful design patterns in rails
 
Rupicon 2014 solid
Rupicon 2014 solidRupicon 2014 solid
Rupicon 2014 solid
 
Rupicon 2014 caching
Rupicon 2014 cachingRupicon 2014 caching
Rupicon 2014 caching
 

Recently uploaded

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 

Recently uploaded (20)

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 

Rupicon 2014 Action pack

  • 1. ActionPack – From request to response 1 / 17
  • 2. What is ActionPack? ActionPack is a: 1. RoR module or component; 2. gem actionpack 4.1.6 3. framework for handling and responding to web requests (the V&C layers in MVC paradigm) 2 / 17
  • 3. It consists of: 2 major components: 1. Action Controller – performing the logic 2. Action View – rendering a template # this will be extracted into a new gem from version 4.1. Now, it only has this big Action Controller, which does all the staf to handle requests and responses for every rails controller. 3 / 17
  • 4. What does ActionController? Action Controller takes the request passed by router and does the groundwork for producing the response. Also uses a set of smart conventions to handle the request (invisible for developer) – RESTful, naming conv., folders, extensions etc. Fetch or save data from a model and use a view (Action View) to create the output. # app/controollers/application_controller.rb class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception end 4 / 17
  • 5. What does ActionView? renders a template, using embedded ruby mingled with HTML or HAML. provides a set of helpers classes for handling common behavior for forms, dates and strings. Some features of Action View are tied to Active Record, but that doesn't mean Action View depends on Active Record. Action View is an independent package that can be used with any sort of Ruby libraries. -- Rails Guides Sample view using en ERB template: <h1>Names of all ruby developers</h1> <% @developers.each do |person| %> Name: <%= person.name %><br> <% end %> 5 / 17
  • 6. I. Action Controller 1. Conventions 2. Parameters 3. Filters 4. Flashes 5. Variants 6 / 17
  • 7. I.1 Conventions Controller naming conventions: camelcase, always ending in ...Controller, plural over/versus singular form of the last but one word. MyApp::Application.routes.draw do resources :developers # mapped to DevelopersController resource :profile # ! ProfilesController end MyApp::Application.routes.draw do resource :profile, controller: 'profile' # override the default plural convention end Methods and actions: snakecase, routing action name = controller method name public methods are most of the time actions move to private visibility all auxiliary methods or filters 7 / 17
  • 8. I.2 Parameters GET, POST, PATCH, PUT parameters go into a new instance of ActionController::Parameters, and this instance is provided by params helper method Hash and Array Parameters routing parameters get '/developers/:status' => 'developers#index', foo: 'bar' # ... URL /developers/active # ... puts params[:status] # => active default_url_options class ApplicationController < ActionController::Base def default_url_options { locale: I18n.locale } end end 8 / 17
  • 9. I.2 Parameters(2) Strong parameters def create Person.create(person_params) end private def person_params params.require(:person).permit(:name, :age) end you can also use permit on nested parameters params.require(:person).permit(:name, { emails: [] }, friends: [ :name, { family: [ :name ], hobbies: [] }]) 9 / 17
  • 10. I.3 Filters are methods that are run before, after or "around" a controller action. are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. Example: class ApplicationController < ActionController::Base before_action :require_login private def require_login unless logged_in? flash[:error] = "You must be logged in to access this section" redirect_to new_login_url # halts request cycle end end end 10 / 17
  • 11. I.3 Filters(2) More examples: skip_[before|after|around]_action, only: or except: class LoginsController < ApplicationController skip_before_action :require_login, only: [:new, :create] end ? follow Open Closed Principle around_action - a before&after filter !? class ChangesController < ApplicationController around_action :wrap_in_transaction, only: :show private def wrap_in_transaction ActiveRecord::Base.transaction do begin yield ensure raise ActiveRecord::Rollback end end 11 / 17
  • 12. I.4 Flashes The flash is a special part of the session which is cleared with each request. It is useful for passing messages (error messsages) form last request. There are two predefined flash keys :notice and :alert, but you can use any other custom keys. Usage examples: class LoginsController < ApplicationController def destroy session[:current_user_id] = nil flash[:notice] = "You have successfully logged out." redirect_to root_url end end or assign a flash message as part of the redirection: redirect_to root_url, notice: "You have successfully logged out." redirect_to root_url, alert: "You're stuck here!" redirect_to root_url, flash: { referral_code: 1234 } 12 / 17
  • 13. I.4 Flashes(2) - useful methods Flashes are available on session only for the next request. But you can alter this default behavior in two ways: 1. using flash.keep method class MainController < ApplicationController # Let's say this action corresponds to root_url, but you want # all requests here to be redirected to UsersController#index. # If an action sets the flash and redirects here, the values # would normally be lost when another redirect happens, but you # can use 'keep' to make it persist for another request. def index # Will persist all flash values. flash.keep # You can also use a key to keep only some kind of value. # flash.keep(:notice) redirect_to users_url end end 13 / 17
  • 14. I.4 Flashes(3) - useful methods 1. using flash.now mehtod class ClientsController < ApplicationController def create @client = Client.new(params[:client]) if @client.save # ... else flash.now[:error] = "Could not save client" render action: "new" end end end rails 4.1 FlashHash now behaves like a HashWithIndifferentAccess. flash[:notice] # => 'Successful login.' flash['notice'] # => 'Successful login.' 14 / 17
  • 15. I.5 ActionPack Variants (rails 4.1) We often want to render different html/json/xml templates for phones, tablets, and desktop browsers. Variants make it easy. The request variant is a specialization of the request format, like :tablet, :phone, or :desktop. It can be set using a before_action : def set_variants request.variant = :tablet if request.user_agent =~ /iPad/ .... end Respond to variants in the action just like you respond to formats: respond_to do |format| format.html do |html| html # renders app/views/projects/show.html.erb html.tablet # renders app/views/projects/show.html+tablet.erb html.phone { extra_setup } # renders app/views/projects/show.html+phone.erb end end 15 / 17
  • 16. I.5 ActionPack Variants(2) Variants also support the common any/all block that formats have. It works for both inline: respond_to do |format| format.html.any { render text: "any" } format.html.phone { render text: "phone" } end and block syntax: respond_to do |format| format.html do |variant| variant.any(:tablet, :phablet) { render text: "ablet html template" } variant.phone { render text: "phone" } end end 16 / 17
  • 17. Resources Rails Guides Rails API doc Action Pack 4.1 changelog 17 / 17