small medium large xlarge

Scottgardner 200x200_pragsmall
09 May 2008, 17:05
Scott Gardner (7 posts)

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} %>

</code>

Scottgardner 200x200_pragsmall
09 May 2008, 21:08
Scott Gardner (7 posts)

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 %>

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

You must be logged in to comment