Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:30:20+00:00 2026-06-14T08:30:20+00:00

I have this model called Post, idaelly I would love to use just ONE

  • 0

I have this model called Post, idaelly I would love to use just ONE partial + ONE layout to achieve the following.

when rendering a single object, outputs:

%div= post.body

and when rendering a collection, outputs:

%ul
  %li= post.body
  %li= post.body
  %li= post.body

Currently I have a partial posts/post.haml looking like: %li= post.body

and whenever I’m rendering a collection, I would do%ul=render @posts

The problems being:

  1. whenever rendering a collection, I have to put the rendering in %ul
  2. The partial is NOT usable for single object without %ul

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.


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 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

  1. </li><li> as a spacer is in no way a good piece of code..
  2. haml would not allow this)

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. div.list-of-posts > div.post instead of ul.posts > li


I know 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 a %ul whenever a collection is passed.

Or else I can do something similar but perhaps a tiny bit 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

This one is the best/cleanest way I can come up with so far, any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:30:22+00:00Added an answer on June 14, 2026 at 8:30 am

    Add another partial which renders the li tag and then invokes the regular partial:

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

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

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

      = div_for post do
        ....
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this simplified scenario, I have a model called Post Each post has a
im having the following issue i have this post action that is called by
So I have a method in a reservation model called add_equip. This method does
Let's say I have a model class called Project, but instead of this: class
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
I have this edmx model (see below) and I would like to add an
I have a model called Vote and this is what vote.rb looks like: class
I have a model called local this model has many portales like this: class
I can't update the view model. I have view called: Overview I have this
I have a model called Post, with a column called vote, and it has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.