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

后端 未结 10 1089
花落未央
花落未央 2020-12-04 09:51

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

<% unless @messages.blank? %>
  <% @messages.each do |message|  %>
    <%# cod         


        
10条回答
  •  忘掉有多难
    2020-12-04 10:14

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

提交回复
热议问题