Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Allow user to specify the partial which will be rendered if the collection is empty or nil

#6020

Hi,

I've think about this a while ago, and it seems like in the real app I've to keep doing this:

<%- unless @posts.empty? %>
  <%= render @posts %>
<%- else -%>
  <%= render "no_post" %>
<%- end -%>

I think there should be the way to accomplish this better. So, I propose the :if_empty option, which user can be specify the partial name to be rendered if the collection is empty. So the code portion about could become this:

<%= render :partial => @posts, :if_empty => "no_post" %>

I've also take it further and add support for :if_empty and :spacer_template in the shorthand version. Those options would be passed in the locals hash in the shorthand version, and would only be use if the first argument is the collection. So, you can do this

<%= render @posts, :if_empty => "no_post", :spacer_template => "post_spacer" %>

I think this will definitely make the collection render in the view looks nicer. I've separate the patch into two parts: the first one with the :if_empty addition only, and the second one for shorthand addition. In case you don't like the way I add to the shorthand, I would love to have at least the first patch got passed.

I'd love to see this got applied to both master and 3-0-stable.

Feedback and suggestion are always welcome. Thanks a lot.

Reported by Prem Sichanugrist (sikachu) · November 20th, 2010 @ 05:02 PM

State: stale
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. José Valim
    José Valim
    • State changed from open to invalid

    I am closing this mate. Feel free to reopen.

    November 25th, 2010 @ 09:47 AM

  2. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • State changed from invalid to open

    Ok, I've update the patch with the version that we've discussed. It will allow the spacer_template to be set in the short-hand version.

    November 25th, 2010 @ 11:03 AM

  3. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 26th, 2011 @ 12:00 AM

  4. rails
    rails
    • State changed from open to stale

    February 26th, 2011 @ 12:00 AM