small medium large xlarge

03 Mar 2010, 02:30
Trung Lê (10 posts)

Sam, can you please make the book code more UJS by providing both code for jQuery and Prototype? I believe that the trend is now moving away from Prototype Helper DLS.

And for whom who is playing around with Rails 3, I have ported some of the ‘depot’ code to UJS and jQuery. To use jQuery, please download latest jquery.min JS and rails.js ( and place both of them in /view/layout/store.html.erb

<%= javascript_include_tag ‘jquery.min’ %> <%= javascript_include_tag ‘rails’ %>

And also append extra line

<%= csrf_meta_tag %>

This tag would do the POST verification to prevent XSS. W/o this tag, the code won’t work.

For the /view/store/index.html.erb, please fix the form tag to

<% form_tag({:action=>'add_to_cart', :id=>product}, :remote=>true) do %>
<%= button_to "Add to Cart" %>
<% end %>

And now for the add_to_cart.js.erb:

To reload the partial, we use this simple jQuery code: $(“#cart”).html(“<%= escape_javascript(render :partial => “cart”, :object => @cart) %>”);

However, there are things MUST be done to make those code work, for Rails 3, the partial now differentiate between JS and HTML partial. HTML partial filename is _partial.html.erb and JS partial is _partial.js.erb. To make the partial work for both format, please rename _cart.html.erb to _cart.erb and _cart_item.html.erb to _cart_item.erb.

That’s just for first few chapters, you can keep going on and porting all current code to UJS. Have fun!

03 Mar 2010, 02:30
Trung Lê (10 posts)

EDIT: jquery.min.js and rails.js must be placed within /public/javascripts

03 Mar 2010, 16:24
Sam Ruby (608 posts)

In the interest of full disclosure, I’m an unabashed fan of JQuery.

That being said, the plan for the next edition is for part II of the book to focus on the Rails defaults, and Part III will discuss alternatives.

Additionally, the scenario itself has been rewritten to follow best practices (namely REST resources). There is no add_to_cart.js.erb any more. My current thoughts on the edition four scenarios can be found here:

A complete set of runs (across multiple versions of Ruby, Rails, and editions of the book can be found here: )

09 Mar 2010, 06:13
Trung Lê (10 posts)

It’s seems to me that it is missing the total_price method for line_item model for

You must be logged in to comment