May 9, 2008
Scott_small Scott Gardner 6 posts

Topic: Agile Web Development with Rails, 3rd Edition / Updating the Image link to add_to_cart for AJAX (pg 130)

Replying to my own topic to report a problem with degrading this functionality when JS is turned off (pg 138). As it stands, using the code I posted above, clicking on the image with JS off does nothing.

Why is that? It’s calling the same action, so the request.xhr? conditional should work the same as clicking the Add to Cart button, right?

I tried refactoring the link as follows, which I got to work but the display got fouled up and my initial attempts to fix the CSS didn’t work (e.g., display: inline;, wrapping it in a new div class, etc.).


<% form_remote_tag :url => {:action => :add_to_cart, :id => product} do %>
    <%= image_submit_tag(product.image_url) %>
<% end %>

Is this the correct approach (and thus I just need to fix my CSS), or is there a better way?

 
May 9, 2008
Scott_small Scott Gardner 6 posts

Topic: Agile Web Development with Rails, 3rd Edition / Updating the Image link to add_to_cart for AJAX (pg 130)

The end of lesson assignments previously had us make the image a link to add_to_cart. Once you switch over to using an Ajax cart, your link will no longer work. Here’s my code to update that link:

depot/app/views/store/index.html.erb


<%= link_to_remote image_tag(product.image_url), :url => {:action => 'add_to_cart', :id => product} %>

 
May 9, 2008
Scott_small Scott Gardner 6 posts

Topic: Agile Web Development with Rails, 3rd Edition / Trying to understand a code example (pg 110, cart.rb)

That really helped, thanks!

 
May 9, 2008
Scott_small Scott Gardner 6 posts

Topic: Agile Web Development with Rails, 3rd Edition / Trying to understand a code example (pg 110, cart.rb)

Thanks for the detailed explanation. I am really just trying to put a handle on this because I realize it’s a key and common usage in Ruby/Rails. I think, for now, I will just have to rely on rote memory of how this works.

Also, can you confirm/correct your statement that this block is in fact an object? According to this explanation on rubylearning.com, a block is not an object unless it is converted to a Proc (e.g., using lambda).

 
May 8, 2008
Scott_small Scott Gardner 6 posts

Topic: Agile Web Development with Rails, 3rd Edition / Trying to understand a code example (pg 110, cart.rb)

The following section of code (excerpted from page 110 depot_g/app/models/cart.rb line 2 in the book, but I’ve included relevant setup code) has_thrown_me_for :a => :loop…


class Cart
    attr_reader :items

    def initialize
        @items = []
    end

    def add_product(product)
        current_item = @items.find {|item| item.product == product}  # say what?!?

Here’s how I understand it:
1. @items is initialized as an empty array
2. add_product receives a product object
3. The @items array is passed to the block
4. Each @items array item is checked to see if its product.id equals the product.id of the product passed to add_product, and if so that matched item is returned to find, which then returns that item to be assigned to current_item

Can someone please confirm that I have this correct? Presuming yes, I still do not understand how the block, which resolves true or false, knows to return the actual item object when a true condition occurs.

Thanks!

 
May 6, 2008
Scott_small Scott Gardner 6 posts

Topic: Agile Web Development with Rails, 3rd Edition / Chapter 3: Installing Rails - Keep it!

In the Note to beta book readers, you mention the possibility of completely removing this chapter, and ask for our feedback. Mine is: update it and keep it! This is valuable info for those of us needing to install or update our Ruby or Rails. Thanks

6 posts