03 Mar 2010, 02:30
308609_10150417437484992_522739991_10780430_6337239_n_pragsmall

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 (http://github.com/rails/jquery-ujs) 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
308609_10150417437484992_522739991_10780430_6337239_n_pragsmall

Trung Lê (10 posts)

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

03 Mar 2010, 16:24
Samr_small_pragsmall

Sam Ruby (584 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: http://intertwingly.net/projects/AWDwR4/checkdepot/

A complete set of runs (across multiple versions of Ruby, Rails, and editions of the book can be found here: http://intertwingly.net/projects/dashboard.html )

09 Mar 2010, 06:13
308609_10150417437484992_522739991_10780430_6337239_n_pragsmall

Trung Lê (10 posts)

It’s seems to me that it is missing the total_price method for line_item model for http://intertwingly.net/projects/AWDwR4/checkdepot/section-10.3.html

  You must be logged in to comment