Rail collection in ul li

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' # list_of_posts %ul= yield # posts/post %li= post.body 

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

 # 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 

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:

 # 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 

Is this the best / cleanest way I can come up with any thoughts?

+6
source share
2 answers

Add another snippet that displays the li tag and then calls the regular partial:

  • Application / views / posts / index.html.haml

     %ul.posts= render collection: @posts, partial: "posts/post_li" 
  • Application / views / posts / _post_li.html.haml

     %li= render post 
  • Application / views / posts / _post.html.haml

     = div_for post do .... 
+1
source

You can have one partial and inside partial, which he can just check to see if local access to it is a collection or the only Post object.

 # Render calls render :partial => "posts/display_posts", :locals => {:posts => Post.first} render :partial => "posts/display_posts", :locals => {:posts => Post.all} # collection 

And your partial:

 - if posts.is_a? Post # single item %div= posts.body - else %ul - posts.each do |post| %li= post.body 
+1
source

All Articles