Dan Mitchell
Dan Mitchell

Reputation: 864

rails if statement refactor

How can I wrap this whole block into an if statement so it only appears if there is at least one @event.speakers.show_in_meet_the_speakers_carousel present?

<div class="sectionTitle speakersColor">Meet the Speakers</div>
    <div id="meetTheSpeaker">
    <% @event.speakers.each do |speaker| %>
    <% if speaker.show_in_meet_the_speakers_carousel.present? %>
        <div class="item">
            <img data-src="<%= speaker.featured_image %>" alt="<%= speaker.featured_image %>" class="lazyOwl">
        <div class="speaker-info-wrapper">
                <p><%= speaker.featured_copy if speaker.featured_copy.present? %></p>
        <h4><%= link_to "Read More", speaker.featured_url %></h4>
        </div>
    </div>
    <% end %>
    <% end %>
</div>

Upvotes: 1

Views: 26

Answers (1)

Nicola Miotto
Nicola Miotto

Reputation: 3706

 <% if @event.speakers.select { |s| s.show_in_meet_the_speakers_carousel.present? }.count > 0 %>
    <div class="sectionTitle speakersColor">Meet the Speakers</div>
        <div id="meetTheSpeaker">

           <% @event.speakers.each do |speaker| %>
           <% if speaker.show_in_meet_the_speakers_carousel.present? %>
               <div class="item">
                   <img data-src="<%= speaker.featured_image %>" alt="<%= speaker.featured_image %>" class="lazyOwl">
               <div class="speaker-info-wrapper">
                       <p><%= speaker.featured_copy if speaker.featured_copy.present? %></p>
               <h4><%= link_to "Read More", speaker.featured_url %></h4>
               </div>
           </div>
           <% end %>
           <% end %>
    </div>
<% end %>

Upvotes: 1

Related Questions