Agile Web Development with Rails 6 Chapter 11. Task F

Chapter 11. Task F: Add a Dash of Ajax

Summary

  • Moved the shopping cart into the sidebar. We then arranged for the create action to re-display the catalog page.
  • We used remote:true to invoke the LineItemsController.create() action using AJAX
  • We used an ERB template to create JavaScript that’ll execute on the the client
  • We wrote a helper method that renders the cart only if it has anything in it
  • We used Action Cable and JavaScript to update the catalog display whenever a product changes
  • We wrote a test that verifies not only the creation of a line item but also the content of the response that’s returned from such a request.

Playtime

//line_items/destroy.js.erbcart = document.getElementById("cart")cart.innerHTML = "<%= j render_if @cart && @cart.line_items.any?, @cart %>"------------------------------------------------------------------//carts/destroy.js.erbcart = document.getElementById("cart")cart.hidden = true;
\\line_items/create.js.erb
cart = document.getElementById("cart")
cart.hidden = false; <--------cart.innerHTML = "<%= j render(@cart) %>"
line_item #edit format.js example
Photo by Susan Q Yin on Unsplash

--

--

--

Seattle Software Engineer. A collector of new skills, with a fondness for all things sushi.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Basic Linux Utilities Every User Should Know

How to fix Amazon prime error code 6085

How to fix Amazon prime error code 6085

Snowflake for BigQuery users — part 2

Game Dev Digest Issue #116 — Do More With Less

Issue #116 - Do More With Less

An Easy and Effective State Management Solution for Flutter to achieve 100 % separation of…

Continuous Delivery with Jenkins

PERI DEX Testnet Pynths Trading Competition

C#, VB.NET Replace Bookmarks in Word

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Roman Turner

Roman Turner

Seattle Software Engineer. A collector of new skills, with a fondness for all things sushi.

More from Medium

Introduction to creating a Sinatra and Active Record.

Notes on #each, #map, and #select in Ruby

Learning Ruby from scratch -part 01

User Auth and Password Protection: Rails