SlideShare a Scribd company logo
Web Application Architectures
Module 5: Middleware
Lecture 7: Rails Controllers–Response
c 2011-13 G.L. Heileman Module 5, Lecture 7 1 / 7
Rails Controllers — Request Processing
In the last lecture we say how the dispatcher routes a request to a
particular controller action (method).
Ex. The HTTP GET request
http://localhost:3000/posts/1
will route to the show method in the PostsController class,
passing params[:id] with a value of 1 to the controller. Note: this
class is defined in the file:
./app/contollers/posts_controller.rb.
Next, the show method will use the ActiveRecord#find method
to retrieve the post with id=1 from the database, and assign it to the
instance variable @post.
Finally, the controller will pass @post to the view, i.e., to the
template file:
./app/views/posts/show.html.erb
and this will be used to create the HTML that will be sent to the
browser.
c 2011-13 G.L. Heileman Module 5, Lecture 7 2 / 7
Filters
The PostsController#show method is defined as follows:
# GET /posts/1
# GET /posts/1.json
def show
end
It doesn’t retrieve the post!
The desired post is actually retrieved from the database using a filter
called set_post.
Filters allow controllers to run shared pre and post processing code
over their methods.
c 2011-13 G.L. Heileman Module 5, Lecture 7 3 / 7
Filters
c 2011-13 G.L. Heileman Module 5, Lecture 7 3 / 7
Sessions
In general, the “state” of an application which needs to persist across
requests should be stored in the database. E.g., posts and comments
are persisted in the database.
There are times when data needs to be persisted differently. E.g., the
current contents of a shopping cart.
Whenever a user connects to a Rails application, a session is created.
Session data is stored in Rails using a hash structure that persists
across requests, and can be accessed by controllers.
Ex. session[:current_user] = user.id
A flash hash is part of the session that is cleared with each request (its
value is made to the next request). A controller can use this to send a
message that can be displayed to the user on the next request.
Ex.
lstinlineflash[: notice] = Postwassuccessfullycreated.
c 2011-13 G.L. Heileman Module 5, Lecture 7 4 / 7
Response – Rendering HTML or JSON
The request:
http://localhost:3000/posts/1
assumes that HTML will be returned. I.e., it’s the same as:
http://localhost:3000/posts/1.html
Rails can return other formats, e.g., JSON capabilities are also
provided by default.
Ex. The following request, will be routed to the same controller
method as before:
http://localhost:3000/posts/1.json
However, it will be rendered using the file:
./app/views/posts/show.json.builder
and JSON will be returned to the client.
c 2011-13 G.L. Heileman Module 5, Lecture 7 5 / 7
Response – Rendering HTML or JSON
c 2011-13 G.L. Heileman Module 5, Lecture 7 5 / 7
Response Format
One last look at rake routes:
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
c 2011-13 G.L. Heileman Module 5, Lecture 7 6 / 7
Redirects
Rails may also respond to an HTTP request using the redirect_to
method.
This method actually tells the browser to send a new request for a
different URL.
Ex. redirect_to ’www.example.com’
Rails has shortcuts for URLs within your application – they’re the
prefix listed when you view routes:
Ex. redirect_to posts_url
will redirect to the index method in the PostsController.
Ex. You can assign a flash message as a part of a redirection:
redirect_to @post, notice: ’Post was successfully created.’
c 2011-13 G.L. Heileman Module 5, Lecture 7 7 / 7

More Related Content

What's hot (20)

Java Components and their applicability in Mule Anypoint Studio
Java Components and their applicability in Mule Anypoint StudioJava Components and their applicability in Mule Anypoint Studio
Java Components and their applicability in Mule Anypoint Studio
VenkataNaveen Kumar
 
Servlet
ServletServlet
Servlet
Rami Nayan
 
Unquoted service path exploitation
Unquoted service path exploitationUnquoted service path exploitation
Unquoted service path exploitation
Dhruv Sharma
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
vamsi krishna
 
Servlet api & servlet http package
Servlet api & servlet http packageServlet api & servlet http package
Servlet api & servlet http package
renukarenuka9
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
smile790243
 
Day3_Part 2_Apache_JMeter_Config_Elements
Day3_Part 2_Apache_JMeter_Config_ElementsDay3_Part 2_Apache_JMeter_Config_Elements
Day3_Part 2_Apache_JMeter_Config_Elements
Sravanthi N
 
Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensions
Soujanya V
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Rami Nayan
 
Inside ASP.NET MVC framework
Inside ASP.NET MVC frameworkInside ASP.NET MVC framework
Inside ASP.NET MVC framework
Ciklum Ukraine
 
Inside asp.net mvc framework
Inside asp.net mvc frameworkInside asp.net mvc framework
Inside asp.net mvc framework
Ciklum Ukraine
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
vikram singh
 
Reading init param
Reading init paramReading init param
Reading init param
Nuha Noor
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Eleonora Ciceri
 
Mule esb add logger to existing flow
Mule esb add logger to existing flowMule esb add logger to existing flow
Mule esb add logger to existing flow
Germano Barba
 
Lecture2
Lecture2Lecture2
Lecture2
Châu Thanh Chương
 
Shell Shock
Shell ShockShell Shock
Shell Shock
Jie Liau
 
Servlet lifecycle
Servlet lifecycleServlet lifecycle
Servlet lifecycle
Durga Devi Thulluri
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
kamal kotecha
 
Servlets - filter, listeners, wrapper, internationalization
Servlets -  filter, listeners, wrapper, internationalizationServlets -  filter, listeners, wrapper, internationalization
Servlets - filter, listeners, wrapper, internationalization
susant sahu
 
Java Components and their applicability in Mule Anypoint Studio
Java Components and their applicability in Mule Anypoint StudioJava Components and their applicability in Mule Anypoint Studio
Java Components and their applicability in Mule Anypoint Studio
VenkataNaveen Kumar
 
Unquoted service path exploitation
Unquoted service path exploitationUnquoted service path exploitation
Unquoted service path exploitation
Dhruv Sharma
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
vamsi krishna
 
Servlet api & servlet http package
Servlet api & servlet http packageServlet api & servlet http package
Servlet api & servlet http package
renukarenuka9
 
692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w692015 programming assignment 1 building a multi­threaded w
692015 programming assignment 1 building a multi­threaded w
smile790243
 
Day3_Part 2_Apache_JMeter_Config_Elements
Day3_Part 2_Apache_JMeter_Config_ElementsDay3_Part 2_Apache_JMeter_Config_Elements
Day3_Part 2_Apache_JMeter_Config_Elements
Sravanthi N
 
Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensions
Soujanya V
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Rami Nayan
 
Inside ASP.NET MVC framework
Inside ASP.NET MVC frameworkInside ASP.NET MVC framework
Inside ASP.NET MVC framework
Ciklum Ukraine
 
Inside asp.net mvc framework
Inside asp.net mvc frameworkInside asp.net mvc framework
Inside asp.net mvc framework
Ciklum Ukraine
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
vikram singh
 
Reading init param
Reading init paramReading init param
Reading init param
Nuha Noor
 
Mule esb add logger to existing flow
Mule esb add logger to existing flowMule esb add logger to existing flow
Mule esb add logger to existing flow
Germano Barba
 
Shell Shock
Shell ShockShell Shock
Shell Shock
Jie Liau
 
Servlets - filter, listeners, wrapper, internationalization
Servlets -  filter, listeners, wrapper, internationalizationServlets -  filter, listeners, wrapper, internationalization
Servlets - filter, listeners, wrapper, internationalization
susant sahu
 

Similar to M5 l7-rails controllers-response-handout (20)

Rupicon 2014 Action pack
Rupicon 2014 Action packRupicon 2014 Action pack
Rupicon 2014 Action pack
rupicon
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
Umair Amjad
 
Drupal8 render pipeline
Drupal8 render pipelineDrupal8 render pipeline
Drupal8 render pipeline
Mahesh Salaria
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
caohansnnuedu
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
Sudip Simkhada
 
Learn Drupal 8 Render Pipeline
Learn Drupal 8 Render PipelineLearn Drupal 8 Render Pipeline
Learn Drupal 8 Render Pipeline
Zyxware Technologies
 
1)Building a MultiThreaded Web ServerIn this lab we will devel
1)Building a MultiThreaded Web ServerIn this lab we will devel1)Building a MultiThreaded Web ServerIn this lab we will devel
1)Building a MultiThreaded Web ServerIn this lab we will devel
AgripinaBeaulieuyw
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
Intro to flask2
Intro to flask2Intro to flask2
Intro to flask2
Mohamed Essam
 
Intro to flask
Intro to flaskIntro to flask
Intro to flask
Mohamed Essam
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
kacie8xcheco
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
nagarajupatangay
 
Asynchronous reading and writing http r equest
Asynchronous reading and writing http r equestAsynchronous reading and writing http r equest
Asynchronous reading and writing http r equest
Pragyanshis Patnaik
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorial
Katy Slemon
 
09 express, moooore
09 express, moooore09 express, moooore
09 express, moooore
Ahmed Elbassel
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
Tamer Rezk
 
Ch2 the application layer protocols_http_3
Ch2 the application layer protocols_http_3Ch2 the application layer protocols_http_3
Ch2 the application layer protocols_http_3
Syed Ariful Islam Emon
 
ajax_pdf
ajax_pdfajax_pdf
ajax_pdf
tutorialsruby
 
ajax_pdf
ajax_pdfajax_pdf
ajax_pdf
tutorialsruby
 
Http programming in play
Http programming in playHttp programming in play
Http programming in play
Knoldus Inc.
 
Rupicon 2014 Action pack
Rupicon 2014 Action packRupicon 2014 Action pack
Rupicon 2014 Action pack
rupicon
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
Umair Amjad
 
Drupal8 render pipeline
Drupal8 render pipelineDrupal8 render pipeline
Drupal8 render pipeline
Mahesh Salaria
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
caohansnnuedu
 
1)Building a MultiThreaded Web ServerIn this lab we will devel
1)Building a MultiThreaded Web ServerIn this lab we will devel1)Building a MultiThreaded Web ServerIn this lab we will devel
1)Building a MultiThreaded Web ServerIn this lab we will devel
AgripinaBeaulieuyw
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docxProject Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
Project Assignment 2 Building a Multi-Threaded Web ServerThis pro.docx
kacie8xcheco
 
Asynchronous reading and writing http r equest
Asynchronous reading and writing http r equestAsynchronous reading and writing http r equest
Asynchronous reading and writing http r equest
Pragyanshis Patnaik
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorial
Katy Slemon
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
Tamer Rezk
 
Ch2 the application layer protocols_http_3
Ch2 the application layer protocols_http_3Ch2 the application layer protocols_http_3
Ch2 the application layer protocols_http_3
Syed Ariful Islam Emon
 
Http programming in play
Http programming in playHttp programming in play
Http programming in play
Knoldus Inc.
 

M5 l7-rails controllers-response-handout

  • 1. Web Application Architectures Module 5: Middleware Lecture 7: Rails Controllers–Response c 2011-13 G.L. Heileman Module 5, Lecture 7 1 / 7
  • 2. Rails Controllers — Request Processing In the last lecture we say how the dispatcher routes a request to a particular controller action (method). Ex. The HTTP GET request http://localhost:3000/posts/1 will route to the show method in the PostsController class, passing params[:id] with a value of 1 to the controller. Note: this class is defined in the file: ./app/contollers/posts_controller.rb. Next, the show method will use the ActiveRecord#find method to retrieve the post with id=1 from the database, and assign it to the instance variable @post. Finally, the controller will pass @post to the view, i.e., to the template file: ./app/views/posts/show.html.erb and this will be used to create the HTML that will be sent to the browser. c 2011-13 G.L. Heileman Module 5, Lecture 7 2 / 7
  • 3. Filters The PostsController#show method is defined as follows: # GET /posts/1 # GET /posts/1.json def show end It doesn’t retrieve the post! The desired post is actually retrieved from the database using a filter called set_post. Filters allow controllers to run shared pre and post processing code over their methods. c 2011-13 G.L. Heileman Module 5, Lecture 7 3 / 7
  • 4. Filters c 2011-13 G.L. Heileman Module 5, Lecture 7 3 / 7
  • 5. Sessions In general, the “state” of an application which needs to persist across requests should be stored in the database. E.g., posts and comments are persisted in the database. There are times when data needs to be persisted differently. E.g., the current contents of a shopping cart. Whenever a user connects to a Rails application, a session is created. Session data is stored in Rails using a hash structure that persists across requests, and can be accessed by controllers. Ex. session[:current_user] = user.id A flash hash is part of the session that is cleared with each request (its value is made to the next request). A controller can use this to send a message that can be displayed to the user on the next request. Ex. lstinlineflash[: notice] = Postwassuccessfullycreated. c 2011-13 G.L. Heileman Module 5, Lecture 7 4 / 7
  • 6. Response – Rendering HTML or JSON The request: http://localhost:3000/posts/1 assumes that HTML will be returned. I.e., it’s the same as: http://localhost:3000/posts/1.html Rails can return other formats, e.g., JSON capabilities are also provided by default. Ex. The following request, will be routed to the same controller method as before: http://localhost:3000/posts/1.json However, it will be rendered using the file: ./app/views/posts/show.json.builder and JSON will be returned to the client. c 2011-13 G.L. Heileman Module 5, Lecture 7 5 / 7
  • 7. Response – Rendering HTML or JSON c 2011-13 G.L. Heileman Module 5, Lecture 7 5 / 7
  • 8. Response Format One last look at rake routes: Prefix Verb URI Pattern Controller#Action posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy c 2011-13 G.L. Heileman Module 5, Lecture 7 6 / 7
  • 9. Redirects Rails may also respond to an HTTP request using the redirect_to method. This method actually tells the browser to send a new request for a different URL. Ex. redirect_to ’www.example.com’ Rails has shortcuts for URLs within your application – they’re the prefix listed when you view routes: Ex. redirect_to posts_url will redirect to the index method in the PostsController. Ex. You can assign a flash message as a part of a redirection: redirect_to @post, notice: ’Post was successfully created.’ c 2011-13 G.L. Heileman Module 5, Lecture 7 7 / 7
  翻译: