I have this model called Post, idaelly. I would like to use only ONE partial + ONE layout to achieve the following.
when rendering a single object, the outputs:
%div= post.body
and when rendering the collection the outputs are:
%ul %li= post.body %li= post.body %li= post.body
I currently have partial posts /post.haml similar to: %li= post.body
and whenever I create a collection I would do %ul=render @posts
Problems:
- when rendering the collection, I have to put the rendering in% ul
- Partial is NOT used for a single object without% ul
Although in 90% of my use cases I present a collection of messages, it does not make sense that the mail is partially not used as a separate template.
I thought I could do something like
# view render partial: 'post', collection: @posts, layout: 'list_of_posts'
This will solve my first problem IF IT WORKS, but it is not. Obviously, render_collection does not accept a layout option, so it is almost a dead end from what I can find in the rendering collection. (Spacer_template may work, but
</li><li> as a spacer, is by no means a good piece of code.- haml would not allow this)
As for my second problem, the easy way out is to do everything in a div, but I'm really reluctant to do this when everything should be on the list. But in order to make it work, this is probably the only clean solution. div.list-of-posts > div.post instead of ul.posts > li
I know that I can always do something like
but in this case, I still need to put% ul whenever the collection is passed.
Or I can do something like this, but maybe a little cleaner:
Is this the best / cleanest way I can come up with any thoughts?