Basti B.
Basti B.

Reputation: 391

To iterate by block in rails

I used Ruby on Rails and I have created more of 40 hiraganas flashcards. In my index view, I do an iteration like this. Some of Hiraganas cards have different I want to create different columns which host different color cards.

I want to iterate on letter A = green, E = red, I = Pink, etc

enter image description here

I only do that

<% @hiraganas.each do |hiragana| %>
  <li>
    <%= render 'allhiraganas', hiragana: hiragana %>
  </li>
<% end %>

Do you know how to select only few elements to iterate. I see some tutos but I try .select

here is the render flashcards code

<div class="row">
  <ul class="list-inline text-center card-frame">
    <li>
      <div class="card">
        <div class="front">
          <% if current_user.try(:admin?) %>
            <%= link_to hiragana_path(hiragana), class:'trash-hiragana', data: { confirm: 'Are you sure?' }, method: :delete do %>
            <%= image_tag("delete-btn.png") %>
            <% end %>
          <% end %>
        <!-- PARAMETRER LE RESPONSIVE BOOTSTRAP -->
        <!-- <div class="col-sm- col-xs-4 col-md-3"> -->
          <span class="card-question popover-word" data-content="<h4 class='text-center letter-uppercase'><%= hiragana.bigletter.upcase %></h4><p class='text-center'><b><%= hiragana.midletter %></b> comme dans <b><%= hiragana.transcription %></b></p>">
            <i class="fa fa-eye fa-lg"></i>
          </span>

          <!-- son de l'hiragana -->
          <span class="audioclick popover-word" data-content="<p class='text-center'><b>le son arrive prochainement !</b></p>">
            <i class="fa fa-volume-up fa-lg"></i>
          </span>
          <!-- image mnémotechnique -->
          <span class="idea popover-word" data-content="<p class='text-center'><b>l'image est bientôt prête !</b><%= hiragana.upload %></p>">
            <i class="fa fa-lightbulb-o fa-lg"></i>
          </span>
          <!-- <div class="prononciation">
            <i class="fa fa-comment"></i>
          </div> -->

          <div class="card-hiragana hiragana-<%=hiragana.bigletter.downcase.last%>">
            <h1><b><%= hiragana.ideo1 %></b></h1>
          </div>

          <div class="card-katakana">
            <p><%= hiragana.ideo2 %></p>
          </div>
          <div id="favorite_<%=hiragana.id%>">
            <%= render 'favs/favorites', hiragana: hiragana %>
          </div>
        </div>
        <div class="back">
          <div class="col-sm-3 col-xs-4 col-md-3 containerbackcards-<%=hiragana.bigletter.downcase.last%>">
            <div class="backcard-hiragana">
              <h1><b><%= hiragana.ideo1 %></b></h1>
            </div>
            <div class="card-bigletter">
              <h4><%= hiragana.bigletter.upcase %></h4>
            </div>
          </div>
        </div>
      </div>
    </li>
  </ul>
</div>

Thank you for your answer

Upvotes: 1

Views: 146

Answers (2)

William Bode
William Bode

Reputation: 74

It depends: If you have the color saved in the hiragana model, then you just need to:

greenhiriganas = Hirigana.where(color: "green")

If you just know in your head, which letters have which colors, then for each section you need to write an array containing all the names

#if for example A, Ka and Shi are green
green = [:a, :ka, :shi]
#then select via include?-method
greenhiriganas = Hirigana.select{ |hiri| green.include?(hiri.name) }

Upvotes: 0

toddmetheny
toddmetheny

Reputation: 4443

which ones do you want? what's the logic for which you'd like to choose? If you simply wanted the first 3 you could say

 @hiriganas = Hirigana.first(3)

if you have a condition you'd like to choose them by select is a good choice.

 @hiriganas = @hiriganas.select{ |hir|  # criteria for selection }

Upvotes: 1

Related Questions