Jngai1297
Jngai1297

Reputation: 2495

Querying Data in the View

I got a pics table that has many tags through pic tags.

Here in the view I am bringing out all the pics

its a bit cut off

<% @pics.each do |i| %>
  <div class="two-col inline-element">
    <div class="col-md-12 white-container no-padding">
      <%= link_to image_tag(i.picture.re_sized.url, alt: i.title), pic_path(i) %>
      <%= i.tags.category%> #my attempt 

I am trying to find the tags to belong to this specific pic which is bind to the i object.

I have better errors installed so when I do something like this

i.tags.all

I am getting this

[#<Tag id: 3, category: "Length", name: "stupid", created_at: "2014-09-12 17:18:44", updated_at: "2014-09-12 17:18:44">] 

I want to query out the values for both category and name.

I have tried a variation of

i.tags.all.category 

got

!! #<NoMethodError: undefined method `category' for #<Array:0x007fabc20c01a0>>

and

i.tags.all["category"]

!! #<TypeError: no implicit conversion of String into Integer>

what is the proper way to do this?

Upvotes: 1

Views: 18

Answers (1)

fivedigit
fivedigit

Reputation: 18682

Since i.tags is a collection, you cannot call category or name on it. You need to do it for each member of the collection.

You can do something like this in your view to display the category and name for each of the tags belonging to i:

<% i.tags.each do |tag| %>
  <%= tag.category %>
  <%= tag.name %>
<% end %>

Upvotes: 1

Related Questions