title: Rails render collection in ul li
body: <p>I have this model called Post, idaelly I would love to use just ONE partial + ONE layout to achieve the following.</p>
  
  <p>when rendering a single object, outputs:</p>
  
  <pre><code>%div= post.body
  </code></pre>
  
  <p>and when rendering a collection, outputs:</p>
  
  <pre><code>%ul
    %li= post.body
    %li= post.body
    %li= post.body
  </code></pre>
  
  <hr>
  
  <p>Currently I have a partial posts/post.haml looking like: <code>%li= post.body</code></p>
  
  <p>and whenever I'm rendering a collection, I would do<code>%ul=render @posts</code></p>
  
  <p>The problems being:</p>
  
  <ol>
  <li>whenever rendering a collection, I have to put the rendering in %ul</li>
  <li>The partial is NOT usable for single object without %ul</li>
  </ol>
  
  <p>Although in 90% of my use cases I'm rendering a collection of post, it doesn't make sense for a post partial not to be usable as a stand-alone template.</p>
  
  <hr>
  
  <p>I thought I could do something like</p>
  
  <pre><code># view
  render partial: 'post', collection: @posts, layout: 'list_of_posts'
  
  # list_of_posts
  %ul= yield
  
  # posts/post
  %li= post.body
  </code></pre>
  
  <p>This would solve my first problem IF IT WORKS, but it doesn't. Apparently render_collection does not take the layout option, so it's practically a dead end from what I can find on rendering collection. (Spacer_template could potentially work, but </p>
  
  <ol>
  <li><code>&lt;/li&gt;&lt;li&gt;</code> as a spacer is in no way a good piece of code.. </li>
  <li>haml would not allow this)</li>
  </ol>
  
  <hr>
  
  <p>As for my second problem, an easy way out would be rendering everything in divs, but I am really reluctant to do so when things should be in a list. But for the sake of making it work, it's probably the only clean-ish solution. <code>div.list-of-posts &gt; div.post</code> instead of <code>ul.posts &gt; li</code></p>
  
  <hr>
  
  <p>I know I can always do something like</p>
  
  <pre><code># view - collection
  %ul= render @posts, in_list: true
  
  # view - object
  = render @post
  
  # posts/post.haml
  - post_counter ||= false
  - tag = post_counter ? :li : :div
  = content_tag tag do
    = post.body
  </code></pre>
  
  <p>but in this case I still need to put a %ul whenever a collection is passed.</p>
  
  <p>Or else I can do something similar but perhaps a tiny bit cleaner:</p>
  
  <pre><code># view - collection of objects
  = render 'posts', posts: @posts
  
  # view - object
  = render @post
  
  # posts/posts.haml
  %ul
    = render post
  
  # posts/post.haml
  - post_counter ||= false
  - tag = post_counter ? :li : :div
  = content_tag tag do
    = post.body
  </code></pre>
  
  <p>This one is the best/cleanest way I can come up with so far, any thoughts?</p>