SlideShare une entreprise Scribd logo
1  sur  6
Rails and XMLHttpRequests




    http://extremia.fi/ • http://extremia.net/
Rails and XMLHttpRequests
• Rails provides multiple ways to handle the actual
  response building from re-rendering the html to
  simply rendering json/xml/whatever-serialized.
• It also provides a handy way to specify some link
  or a form that has an ajax action.
• Adding :remote => true to i.e. a link_to-helper
  will transform the link to fire an XMLHttpRequest
  instead of following the link.
• This allows pure Rails applications to be built in a
  way that degrades gracefully if the client doesn’t
  have JavaScript enabled.
             http://extremia.fi/ • http://extremia.net/
Handling the request
• Rails provides a lot of control over how to
  handle MLHttpRequests:
  – It’s possible render a partial view and return that
    using some logic already defined on the client to
    update the DOM, render JSON and parse it into
    the DOM on the client,
  – or return JavaScript containing all the logic
    required to update the currently visible page on
    the client.
            http://extremia.fi/ • http://extremia.net/
Handling the request
• To check whether the request is an XHR one Rails
  provides a helper called request.xhr?
  – It checks the request for ‘X-Request-With’ =
    ‘XMLHttpRequest’ header. So if your application
    handles ajax for example by re-rendering a partial
    view you could use request.xhr? to check whether the
    whole page should be rendered or not.
• A bit cleaner way is to use respond_to with
  format. By default XHR-requests made using the
  Rails helpers with :remote => true can be handled
  using format.js.
            http://extremia.fi/ • http://extremia.net/
Handling the response
• Apart from returning JavaScript that updates
  the DOM straight from server the response
  can be handled using events .
• Requests that happen using the Rails
  unobtrusive JavaScript (ie. :remote => true)
  fire events
  (ajax:beforeSend, ajax:complete, ajax:success
  , ajax:error) allows you to use for
  example, jQuery to bind to them, updating the
  DOM based on the request status.
           http://extremia.fi/ • http://extremia.net/
Handling the response
• Rails also allows you to return JavaScript that’s
  executed on response, allowing you to write
  code for updating the DOM to be returned
  with the changed data.
• Handling XHR this way probably won’t scale
  too far as the response is always created on
  the server side and requires more data to be
  sent over the wire in comparison to serialized
  json/xml and client side rendering.
            http://extremia.fi/ • http://extremia.net/

Contenu connexe

Dernier

Dernier (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Rails and xmlhttprequests

  • 1. Rails and XMLHttpRequests http://extremia.fi/ • http://extremia.net/
  • 2. Rails and XMLHttpRequests • Rails provides multiple ways to handle the actual response building from re-rendering the html to simply rendering json/xml/whatever-serialized. • It also provides a handy way to specify some link or a form that has an ajax action. • Adding :remote => true to i.e. a link_to-helper will transform the link to fire an XMLHttpRequest instead of following the link. • This allows pure Rails applications to be built in a way that degrades gracefully if the client doesn’t have JavaScript enabled. http://extremia.fi/ • http://extremia.net/
  • 3. Handling the request • Rails provides a lot of control over how to handle MLHttpRequests: – It’s possible render a partial view and return that using some logic already defined on the client to update the DOM, render JSON and parse it into the DOM on the client, – or return JavaScript containing all the logic required to update the currently visible page on the client. http://extremia.fi/ • http://extremia.net/
  • 4. Handling the request • To check whether the request is an XHR one Rails provides a helper called request.xhr? – It checks the request for ‘X-Request-With’ = ‘XMLHttpRequest’ header. So if your application handles ajax for example by re-rendering a partial view you could use request.xhr? to check whether the whole page should be rendered or not. • A bit cleaner way is to use respond_to with format. By default XHR-requests made using the Rails helpers with :remote => true can be handled using format.js. http://extremia.fi/ • http://extremia.net/
  • 5. Handling the response • Apart from returning JavaScript that updates the DOM straight from server the response can be handled using events . • Requests that happen using the Rails unobtrusive JavaScript (ie. :remote => true) fire events (ajax:beforeSend, ajax:complete, ajax:success , ajax:error) allows you to use for example, jQuery to bind to them, updating the DOM based on the request status. http://extremia.fi/ • http://extremia.net/
  • 6. Handling the response • Rails also allows you to return JavaScript that’s executed on response, allowing you to write code for updating the DOM to be returned with the changed data. • Handling XHR this way probably won’t scale too far as the response is always created on the server side and requires more data to be sent over the wire in comparison to serialized json/xml and client side rendering. http://extremia.fi/ • http://extremia.net/