Sathish Manohar
Sathish Manohar

Reputation: 6119

How do I prevent the empty instance from appearing in the view?

I have topics_controller.rb that creates a new @reply instance variable for the inline form in the show page.

def show
  @topic = Topic.find(params[:id])
  @replies = @topic.replies
  @reply = @topic.replies.new
end

Now when I loop through @replies to display and style existing replies, An empty div appears because of the new instance of the reply in controller.

<% @replies.each do |reply| %>
    <div class="reply span8">
      <%= reply.body %>
    </div>
<% end %>

How do I tackle this and hide the @topic.replies.new instance from showing up?

Upvotes: 0

Views: 42

Answers (2)

hawk
hawk

Reputation: 5408

You can easily create new instance directly:

def show
  @topic = Topic.find(params[:id])
  @replies = @topic.replies
  @reply = Reply.new(topic_id: params[:id])
end

Upvotes: 0

Benjamin Tan Wei Hao
Benjamin Tan Wei Hao

Reputation: 9691

Try this:

<% @replies.each do |reply| %>
  <% next unless reply.body %>
    <div class="reply span8">
      <%= reply.body %>
    </div>
<% end %>

Upvotes: 1

Related Questions