Toran Billups
Toran Billups

Reputation: 27399

How to display a collection of objects in rails 3

So I'm implementing my first many-to-many relationship and I'm having some trouble getting the related collection to populate in my view

For example: simple blog w/ posts and tags

my post controller new action has this var

@tags = Tag.all

next inside my view I have the following loop

<% for tag in @tags %>
    <input type="checkbox" id="<%=tag.id%>" value="<%=tag.id %>"><%=tag.description%>
<% end %>

In the MySQL database I have 2 tags listed but for some reason in the view they don't show (not even a single checkbox input so I assume my loop syntax is invalid)

Anything else I missed here?

Upvotes: 1

Views: 655

Answers (1)

qpingu
qpingu

Reputation: 960

Might be a problem with the new Active Record in Rails 3. Try:

<% @tags.each do |tag| %>

It may look like it's just another method of looping over elements, but I've already had a few issues with Ruby acting on the ActiveQuery object instead of the desired query results.

Upvotes: 3

Related Questions