I have this in my view code:
<h2>What Others Are Saying...</h2>
<% @comments.each do |comment| %>
<ul>
<li><%= comment.email %></li>
<li><%= comment.body %></li>
</ul>
<% end %>
<hr>
<%= render "comments/comment_form" %>
controller code:
before_filter :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
@comment = @post.comments.build
@comments = @post.comments
end
When there are no comments, I still see the ul > li in my DOM, is there a way to not render this until soemone has actually posted a message?
Your
@commentsvariable is not empty. When database return zero comments, you still have a new comment instance in@commentsarray. That is the reason your HTML code is being rendered while you see no comments. You can use the following codeUPDATE