06 Mar 2013, 21:49
Generic-user-small

Jason Landry (3 posts)

There are three lines of javascript/jquery code that makes clicks on the product images add to cart. I have tried for hours to get this to work to no avail.

Here’s the code in question: ` $(document).on “page:change”, -> $(‘.store .entry > img’).click -> $(this).parent().find(‘:submit’).click() ` Where should I start debugging this? I tried putting

alert “I’m here” between the first and second line, and it never gets called. That leads me to believe that the on(page:change) isn’t working properly.

Help!

06 Mar 2013, 21:59
Generic-user-small

Jason Landry (3 posts)

Just tried it with the downloadable source code. Doesn’t work with that either in Firefox or Chrome. The normal “add to cart” button works fine in both.

07 Mar 2013, 03:26
Samr_small_pragsmall

Sam Ruby (584 posts)

Looks like turbo links isn’t working. One way to be sure is to see if changing $(document).on "page:change", to simply $ “fixes” it.

If works, change it back (as you really want turbo links to work), and check the following lines:

Gemfile:# Read more: https://github.com/rails/turbolinks
Gemfile:gem 'turbolinks'
app/views/layouts/application.html.erb:    "data-turbolinks-track" => true %>
app/views/layouts/application.html.erb:  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
app/assets/javascripts/application.js://= require turbolinks
07 Mar 2013, 14:16
Generic-user-small

Jason Landry (3 posts)

Thanks for the help Sam. Change the “page:change” to line to simply $ did fix it - clicking on images did add product to the cart.

So I checked the other files, and everything that should be there, is.

In application.html.erb:

  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>

</code>

In Gemfile:

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

</code>

And in application.js:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require turbolinks
//= require_tree .

</code>

I’m sure I’m missing something ridiculously easy, but I’m a newbie with Rails.

10 Mar 2013, 22:16
Generic-user-small

mike-b (2 posts)

I also am experiencing the exact same thing as Jason.

11 Mar 2013, 00:04
Samr_small_pragsmall

Sam Ruby (584 posts)

A version that should work for everybody:

$(document).on "ready page:change", ->
13 Mar 2013, 21:16
Generic-user-small

Sean Coleman (3 posts)

That worked for me.

So for Rails 4.0 with Ruby 2.0, it seems as if the turbolinks evens are not firing. Is that what’s going on?

14 Mar 2013, 13:34
Samr_small_pragsmall

Sam Ruby (584 posts)

Inclusion of Turbolinks is new with Rails 4.0. It works with Ruby 2.0.

What’s going on is that turbolinks speeds things up when you navigate from one page within your site to another page within your site. It doesn’t do anything unless your previous page is within this site.

The scenario in this section is dealing with the front page of your site… so most of the time you won’t see turbolinks events, but rather the traditional jquery/dom events.

So this is effectively an errata on the book, and the fix (which I’ve already applied to my source) is as indicated above and to add a few sentences of explanation. This will be pushed out with the next update (ideally concurrent with or shortly after rails beta2).

So: thanks for the feedback, it is making the book better!

14 Mar 2013, 13:13
Generic-user-small

Sean Coleman (3 posts)

Sam, thanks a bunch for the explanation.

  You must be logged in to comment