small medium large xlarge

22 May 2017, 15:50
Patrício João dos Santos (6 posts)

At Iteration F2, Pag 157 we have this code:

$('#cart').html("<%=j render(@cart) %>"); For those who are starting, this code could not work, because new rails does not come with jquery by default. So what you need to do is add jquery.

First add jquery using yarn:

$ bin/yarn add jquery

Then add a require statement at app/javascripts/application.js file:

//= require rails-ujs //= require turbolinks //= require jquery //= require_tree .

13 Jun 2017, 20:53
Karen Hoofnagle (1 post)

You need to backout a touch further. People may not have yarn installed at all, in which case they’ll need to visit for install instructions first.

20 Jul 2017, 12:51
John Yeates (8 posts)

Yarn is now installed as part of the installation instructions in chapter 1, so that shouldn’t be a problem.

However, if you add jquery through yarn, it doesn’t seem to load jquery-ui-rails correctly and your application will throw an error on each page load. Adding gem jquery-rails to the Gemfile instead worked for me.

11 Oct 2017, 17:12 (1 post)

As John Yeates said,

gem 'jquery-rails

in the Gemfile works, however I still had to add the following in application.js

//= require jquery
//= require jquery_ujs
//= require_tree .

It was really confusing why the Javascript Ajax didn’t work, it would be nice if the book could mention that we need to add those lines in our code. I had to diff the files between depot_l and my code in order to spot the 2 lines of difference :(

11 Oct 2017, 17:47
David Copeland (467 posts)

With the latest/final version of the book, jQuery should not be needed at all. The book does not reference it any longer.

You must be logged in to comment