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

Listed IFC ART NFT Price : 0.004 ETH

Four Areas of Extreme Programming

Breaking problems down

Introduction of WebSockets

Golang — Syntax Review

Google foobar as a non-developer Level 3

Make Command-key Ctrl on Windows 10 — Just Like Mac

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

How to implement SOAP API Client in Ruby on Rails?

Object Calisthenics in Ruby

Live Chat Implemented with Action Cable in a React/Rails Application

The making of an Art Auction House