Rails: An elegant way to display a message when there are no elements in database

Ruby on-RailsRubyLoopsIterator

Ruby on-Rails Problem Overview


I realized that I'm writing a lot of code similar to this one:

<% unless @messages.blank? %>
  <% @messages.each do |message|  %>
    <%# code or partial to display the message %>
  <% end %>
<% else %>
  You have no messages.
<% end %>

Is there any construct in Ruby and/or Rails that would let me skip that first condition? So that would be executed when iterator/loop won't enter even once? For example:

<% @messages.each do |message| %>
  <%# code or partial to display the message %>
<% and_if_it_was_blank %>
  You have no messages.
<% end %>

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You could also write something like this:

<% if @messages.each do |message| %>
  <%# code or partial to display the message %>
<% end.empty? %>
  You have no messages.
<% end %>

Solution 2 - Ruby on-Rails

If you use the :collection parameter to render e.g. render :partial => 'message', :collection => @messages then the call to render will return nil if the collection is empty. This can then be incorporated into an || expression e.g.

<%= render(:partial => 'message', :collection => @messages) || 'You have no messages' %>

In case you haven't come across it before, render :collection renders a collection using the same partial for each element, making each element of @messages available through the local variable message as it builds up the complete response. You can also specify a divider to be rendered in between each element using :spacer_template => "message_divider"

Solution 3 - Ruby on-Rails

I'm surprised my favorite answer isn't up here. There is an answer thats close, but I don't like bare text and using content_for is klunky. Try this one on for size:

  <%= render(@user.recipes) || content_tag("p") do %>
    This user hasn't added any recipes yet!
  <% end %>

Solution 4 - Ruby on-Rails

One way is to do something like:

<%= render(:partial => @messages) || render('no_messages') %>

Edit:

If I remember correctly this was made possible by this commit:

http://github.com/rails/rails/commit/a8ece12fe2ac7838407954453e0d31af6186a5db

Solution 5 - Ruby on-Rails

You can create some custom helper. The following one is just an example.

# application_helper.html.erb
def unless_empty(collection, message = "You have no messages", &block)
  if collection.empty?
    concat(message)
  else
    concat(capture(&block))
  end
end

# view.html.erb
<% unless_empty @messages do %>
  <%# code or partial to dispaly the message %>
<% end %>

Solution 6 - Ruby on-Rails

As a note, you may as well just iterate over an empty array if you're looking for efficiency of expression:

<% @messages.each do |message|  %>
  <%# code or partial to dispaly the message %>
<% end %>
<% if (@messages.blank?) %>
  You have no messages.
<% end %>

While this does not handle @messages being nil, it should work for most situations. Introducing irregular extensions to what should be a routine view is probably complicating an otherwise simple thing.

What might be a better approach is to define a partial and a helper to render "empty" sections if these are reasonably complex:

<% render_each(:message) do |message|  %>
  <%# code or partial to dispaly the message %>
<% end %>

# common/empty/_messages.erb
You have no messages.

Where you might define this as:

def render_each(item, &block)
  plural = "#{item.to_s.pluralize}"
  items = instance_variable_get("@#{plural}")
  if (items.blank?)
    render(:partial => "common/empty/#{plural}")
  else
    items.each(&block)
  end
end

Solution 7 - Ruby on-Rails

Old topic but I didn't really like any of these so playing around on Rails 3.2 I figured out this alternative:

<% content_for :no_messages do %>
  <p>
    <strong>No Messages Found</strong>
  </p>
<% end %>
    
<%= render @messages || content_for(:no_messages) %>

Or if you need a more verbose render with partial path like I did:

<%= render(:partial => 'messages', 
     :collection => @user.messages) || content_for(:no_messages) %>

This way you can style the "no messages" part with whatever HTML / view logic you want and keep it nice a easy to read.

Solution 8 - Ruby on-Rails

That code can be shortened to:

<%= @messages.empty? ? 'You have no messages.' : @messages.collect { |msg| formatted_msg(msg) }.join(msg_delimiter) %>

Comments:

formatted_msg() - helper method which adds formatting to the message

msg_delimiter - variable containing delimiter like "\n" or "<br />"

BTW I'd suggest to use empty? method instead of blank? for checking an array, because a) its name is more concise :) and b) blank? is an ActiveSupport extension method which won't work outside Rails.

Solution 9 - Ruby on-Rails

You could split up your two cases into different templates: one if messages exist and one if no message exist. In the controller action (MessagesController#index probably), add as the last statement:

render :action => 'index_empty' if @messages.blank?

If there are no messages, it'll display app/views/messages/index_empty.html.erb. If there are messages, it'll fall through and display app/views/messages/index.html.erb as usual.

If you need this in more than just one action, you can nicely refactor it into a helper method like the following (untested):

def render_action_or_empty (collection, options = {})
    template = params[:template] || "#{params[:controller]}/#{params[:action]}"
    template << '_empty' if collection.blank?
    render options.reverse_merge { :template => template }
end

With this, you just need to put render_action_or_empty(@var) at the end of any controller action and it'll display either the 'action' template or the 'action_empty' template if your collection is empty. It should also be easy to make this work with partials instead of action templates.

Solution 10 - Ruby on-Rails

Below solution works for me because I want tr and td with colspan property:

<%= render(@audit_logs) || content_tag(:tr, content_tag(:td, 'No records',colspan: "11"))%>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub TroszokView Question on Stackoverflow
Solution 1 - Ruby on-RailsFernando AllenView Answer on Stackoverflow
Solution 2 - Ruby on-RailsmikejView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDavid BockView Answer on Stackoverflow
Solution 4 - Ruby on-RailsjonniiView Answer on Stackoverflow
Solution 5 - Ruby on-RailsSimone CarlettiView Answer on Stackoverflow
Solution 6 - Ruby on-RailstadmanView Answer on Stackoverflow
Solution 7 - Ruby on-RailsKanshaView Answer on Stackoverflow
Solution 8 - Ruby on-RailsLukas StejskalView Answer on Stackoverflow
Solution 9 - Ruby on-RailsZargonyView Answer on Stackoverflow
Solution 10 - Ruby on-RailsJigar BhattView Answer on Stackoverflow