dcalixto
dcalixto

Reputation: 411

Iterate and Stringify_keys

Alright, I was very happy iterating over items table when suddenly BANG!

NoMethodError in Items#index

undefined method `stringify_keys' for "/items/7":String

Why like this?

Item Controller

def index 
  @item = current_user.bar.items.all
end

Item Index View

<% current_user.bar.items.each do |item|%>
    <tr>
        <td><%= link_to(image_tag( item.foto.url.to_s), item.name, item_path(item.id)) %></td>
    </tr >
<% end %>

Upvotes: 0

Views: 275

Answers (2)

Lee Jarvis
Lee Jarvis

Reputation: 16241

Check out the method signature for link_to.

You're handing it an image, some text, and a path. That's wrong. The reason you're getting that error is because link_to thinks that the last argument is an options hash, when really, it's a string (the path). You need to remove the image_tag, the item.name, or simply provide a block to include them both:

<%= link_to item_path(item) do %>
  <%= image_tag(item.foto.url) %> <%= item.name %>
<% end %>

Upvotes: 3

Phyo Wai Win
Phyo Wai Win

Reputation: 1160

Your use of link_to method is not right. It should be something like this (if you want image to be the 'clickable image')

<%= link_to image_tag( item.foto.url.to_s), item_path(item.id)) %>

Hope it helps.

Upvotes: 1

Related Questions