Andrew says:

I changed this line:


<%= image_tag(product.image_url) %>

... to …


<%= link_to(image_tag(product.image_url), 
            line_items_path(:product_id => product), 
            html_options = {:method => :post}) %>

Arun says:

The above works fine, but when I click the image, it directs to line_items show action, instead of cart(http://localhost:3000/line_items?product_id=3). Andrew, did you face this? How to direct it to cart’s show action?

Katja says:

Andrew, this does not work for me

<%= link_to(image_tag(product.image_url), 
            line_items_path(:product_id => product), 
            html_options = {:method => :post}) %>

Had to change the html_options = {:method => :post} into :method => :post and everything was fine.

Arun says:

I did it. Instead of using HTML call to post, I invoked the create method through rails. Works fine.

<%= link_to(image_tag(product.image_url), line_items_path(:product_id =>product), :method=> "create") %>

Martin says:

But finally “user friendly way” is:

<%= link_to image_tag(product.image_url, {:border => "0", :title => "Push it into cart!"}),
                      line_items_path(:product_id => product), {:method => :post} %>

Mike says:

Improving upon what Martin posted, I wanted to store the image border style in my style sheet, so I specified a CSS class using :class

<%= image_tag(product.image_url, :class => "image_link")

And added this to my CSS file:

.image_link {
    border: 0;
}

Mo says:

Not sure why, but with the solutions above, right clicking the image actually invokes the click-action (creating an item_line) and not only the popup menu from the browser (using Firefox). It’s because of the javascript, but that’s beyond my knowledge :-( ... any ideas? Thx!

Bruce says:

@Mo the issue seems to be a bug in Firefox. Try in Safari and you see the expected behaviour.

Jinlin says:

Could anyone explain to me why this works: line_items_path(:product_id => product), :method =>:post

but this won’t work? :controller => ‘line_items’, :method =>:post, :acton=>‘create’

Aaron says:

Updated syntax for Ruby:
<%= link_to(image_tag(product.image_url), line_items_path(product: product), method: :post) %>

Kevin says:

I am using the following:

<%= link_to image_tag(product.image_url), line_items_path(:product_id => product), :method => :post %>

Like Arun above, this takes me to line_items/show and not the cart. I also tried Arun’s suggestion of invoking the create action, but that did not work either. Can anyone tell me if there is something I am missing? Apparently this activity has been removed from the newest version of the book. I heard there are some javascript configuration issues, but I am not sure. If anyone could help it would be greatly appreciated.

Colm says:

Simples…


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

EDIT: This solution is good for the 3rd Edition of the book, but not the 4th as :action => 'add_to_cart' does no seem to be used any longer.

Hi! I’ve tried with all the solutions shown above but none of them worked, and the only thing I get by clicking on the image is a “new line item” instead of “create”. Any ideas? Thanks

derfarg says:

its works but i use javascript for that.

<form name="<%= product.id %>" 
        method='post' action="<%= line_items_path(:product_id => product) %>">

            <%= link_to image_tag(product.image_url), 
                                    "javascript:document.forms[#{product.id}].submit();", 
                                    :method => :post %>
        </form>

GGD says:

I use following code and it works:


<%= link_to image_tag(product.image_url), line_items_path(:product_id => product), :method => :post %>

Chang-Ju Park says:

I use rails 3.2.8, ruby 1.9.3 my code is..

<%= link_to image_tag(item.product.image_url), line_items_path(product_id: item.product), method: :post %>

is it correct?

It’s working good.. on my computer

Arik Malok said:

All above solutions look good, but the problem is with JavaScript. If you followed the example in the book and you have overwrite application.html.erb (as explained in the book), then look in app/view/layout/application.html.erb for this line:


<%= javascript_include_tag :defaults %>

and change this to:


<%= javascript_include_tag "application" %>

otherwise it will not load the JavaScript files.

Rafael said:

<%= link_to image_tag(product.image_url), line_items_path(product_id: product), {method: :post} %>

Works for me with ruby 1.9.3 and rails 3.2.1

Mark said

4th Edition of the book, Ruby 1.9.3 and Rails 3.2.8

Changed app/views/store/index.html.erb link as follows

<%= link_to image_tag(product.image_url), line_items_path(product_id: product), method: :post %>

Works for me. Item added directly to cart when clicking on the image.