Task B: Catalog Display #2

Dave says:

The trick here is working out that you can write

link_to  image_tag("....."), :action => :add_to_cart, :id => ...

This might might more sense if we add parentheses

link_to (image_tag("....."), :action => :add_to_cart, :id => ...)

So the solution is like this:

<%= link_to image_tag(product.image_url), :action => :add_to_cart, :id => product %>

And then maybe add a @border: 0;@ to the depot.css stylesheet (under entry.img) to get rid of the border, or alternatively format it in some greenish color.

Doesn't this method go against everything you've preached thus far in the book about not using GET to modify something on the server?

I agree, this should use the POST method in order to be RESTful:

<%= link_to image_tag(product.image_url), 
      { :action  => :add_to_cart, :id => product },
      :method  => :post %>

GarryFre? says:

The above "RESTful" code, causes the price for the final item in the catalog to be mis-aligned to the right by an inch, and this also slides the add to cart button to the right in turn. Does anyone know why?

Brandon says:

This minor difference (which I picked up from the docs at api.rubyonrails.com) also worked for me. Both variations appear to generate the same html under the covers:

<%= link_to image_tag(product.image_url),
      { :action  => :add_to_cart, :id => product },
      :post  => true %>

***NOTE: Using :post => true causes the following error in log/development.log:

  DEPRECATION WARNING: Passing :post as a link modifier is deprecated. Use :method => "post" 
  instead. :post will be removed in Rails 2.0.  See http://www.rubyonrails.org/deprecation for 
  details. (called from convert_options_to_javascript! at   

Jason says:

I have the following in index.rhtml:

<%= link_to(image_tag(product.image_url), {:action => :add_to_cart, :id => product}, 
      :post => true) %>

But this still produces the following html:

<a href="/store/add_to_cart/6" onclick="var f = document.createElement('form'); 
      f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; 
      f.action = this.href;f.submit();return false;"><img alt="Auto" 
      src="/images/auto.jpg?1168809918" /></a>

When the user moves over the image, it still provides the link that a get request would. Is this the expected functionality?

Also, why wouldn't you use image_tag(product.image_url, :border => 0) vs. CSS?

Augie says:

You want to separate style from content with CSS. Even a little thing like a border tag is mixing the two. That's why it's better to make the border declaration in CSS.

DaveK says:

What does making the product into a separate hash as {:action => :add_to_cart, :id => product} do when you add the :method => :post? I'm guessing it won't work right if you leave the extra {} out, but… why? Thanks for any insight!

Frtz snorts:

The Rails Framework API


claims that the link_to helper has four parameters:

  • name
  • options = {}
  • html_options = nil
  • *parameters_for_method_reference

So if you didn't include the {} around the options, the :id => product would be interpreted as the html_options parameter.

Certainly bad, bad things would result.

Just found this method in the API which also works fine:

link_image_to (product.image_url, :action => :add_to_cart, :id =>product, :method => :post)


link_image_to deprecated. Some thoughts about link_to helper parameters: 1 parameter is body of <A> element 2 parameter is href= attribute value of <A> element

GeorgeGates says:

I had a problem with the link_to working with AJAX, so I ended up with the following:

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

Jorge says:

I actually had the same problem with the link_to method working with AJAX, so I did the same thing except I added a class name to the tag so that I could style it. The old style "#store .entry img" only works for image tags and we no longer have that. With the ":class => 'product_image'" added into it we can now use our old style by modifying the stylesheet to read "#store .entry .product_image" instead of "#store .entry img".

This code in "index.rhtml":

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

This code in "depot.css":

#store .entry .product_image {
  width: 75px;
  padding: .25em;
  float: left;

Nick says:

Using any of the code suggested above seems to lack the inclusion of an authenticity token if you look at the source. Alternatively, if watch the output of your server, you can see that when adding a product to the cart by clicking the image that it shows no token. Any thoughts?

Caruso_g says:

This is my solution, accordingly with Rails 2.2.2 API (15/12/08):

<%= link_to image_tag(product.image_url),
      {:action => :add_to_cart, :id => product },
      :method  => :post,
      :alt => "Click on this book image to add it to the cart", :title => "Add to cart" %>

Kedar Mhaswade says:

All these suggestions made sense to me. I ended up adding a helper method to format the time. I just have something to draw your attention to, i.e. in the ERB template, <!— —> does not mean it is an HTML comment. I learnt it the hard way.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License