rails if语句重构

时间:2014-03-14 14:24:25

标签: ruby-on-rails-3 mongoid

如何将整个块包装到if语句中,以便只有至少有一个 @ event.speakers.show_in_meet_the_speakers_carousel 出现时才会出现?

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

1 个答案:

答案 0 :(得分:1)

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