content_tag_for a collection in Rails?

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-06-01T09:22:23Z Indexed on 2010/06/01 9:23 UTC
Read the original article Hit count: 200

Filed under:

I have a Post model. Posts have many Comments. I want to generate a <ul> element for post.comments using content_tag_for.

Ideally, it'd produce

<ul id="comments_post_7" class="comments">
...
</ul>

where 7 is the ID of the Post.

The closest I can get uses

<% content-tag-for :ul post, :comments do %>

which produces

<ul id="comments_post_7" class="post">
...
</ul>

which is pretty close, except for the class="post". Using :class => :comments in the content_tag_for yields class="post comments", but I just want class="comments".

It seems logical that I'd be able to use something like

<% content_tag_for :ul post.comments do %>

but, sadly, that yields

<ul id="array_2181653100" class="array">
...
</ul>

I've searched far and wide. I feel like I'm missing some elegant way to do this. Am I? Because, seriously, <ul id="comments_post_<%= post.id %>" class="comments"> is painful.

© Stack Overflow or respective owner

Related posts about ruby-on-rails