user3399101
user3399101

Reputation: 1537

How to use unless condition, without taking up space?

So I have a carousel with several movie clip thumbnails, however... if a user doesn't upload a thumbnail but uploads a clip there will be this ugly placeholder image. I'd like to have it so that if a user doesn't upload a thumbnail, it doesn't show the clip at all in the carousel.

I've tried doing this with:

       <% if @clips.any? %>
          <div class="queue_mone">
            <div class="jcarousel-skin-ie7">
              <div style="position: relative;" class="">
                <ul style="overflow: hidden; position: relative;" id="second-carousel" class="webcliplist first-and-second-carousel jcarousel-list jcarousel-list-horizontal">
                  <% @clips.each do |clip| %>
                    <li>
                      <%= link_to clip do %>
                          <%= image_tag(clip.thumbnail.url(:medium)) unless clip.thumbnail.blank? %>
                        <p style="padding-top:5px; font-family: 'SourceSansPro-Regular';"><%= clip.name unless clip.thumbnail.blank? %></p>
                      <% end %>
                    </li>
                  <% end %>

The problem with using unless is that there is a blank space where the clip isn't showing. How can I have it so that, there's no blank space, the clip that does have a thumbnail just moves over and takes the empty thumbnail clips space?

In other words, it's like the clip with no thumbnail is still there but the image and title just isn't showing. I'd like it to be so the clip with a thumbnail goes in the place of the clip with no thumbnail, so there are no blank spaces.

Thanks!

Upvotes: 1

Views: 33

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54902

There is several options for you here:

option #1: no <li> if no thumbnail

<% @clips.each do |clip| %>
  <% next if clip.thumbnail.blank? %>
  <li>
    <%= link_to clip do %>
      <%= image_tag(clip.thumbnail.url(:medium)) %>
      <p style="padding-top:5px; font-family: 'SourceSansPro-Regular';"><%= clip.name %></p>
    <% end %>
  </li>
<% end %>

option #2: empty <li> if no thumbnail

<% @clips.each do |clip| %>
  <li>
    <% if clip.thumbnail.present? %>
      <%= link_to clip do %>
        <%= image_tag(clip.thumbnail.url(:medium)) %>
        <p style="padding-top:5px; font-family: 'SourceSansPro-Regular';"><%= clip.name %></p>
      <% end %>
    <% end %>
  </li>
<% end %>

option #3: no image_tag if no thumbnail BUT link + title

<% @clips.each do |clip| %>
  <li>
    <%= link_to clip do %>
      <%= image_tag(clip.thumbnail.url(:medium)) if clip.thumbnail.present? %>
      <p style="padding-top:5px; font-family: 'SourceSansPro-Regular';"><%= clip.name %></p>
    <% end %>
  </li>
<% end %>

Upvotes: 1

Related Questions