1

私が達成したいこと:私からisotope.jsを使用してカテゴリソーターを動的に作成しますGallery.tag

それをするために私はする必要があります

  1. 私のhtml .classの小文字の一意のタグをループアウトします
  2. タグをループアウトして (翻訳に globalize 3 を使用)、<h2>

@galleries言語を切り替えると、中国語の文字が .class に出力され、ソーターが壊れるため、両方のケースに使用できません。したがって、2 つの配列を作成し、それらを zip で結合し、それぞれをループさせます。

アルファベット順に並べ替えようとしましたが、対応する中国語のタグが英語の順序に従っていません。

ギャラリー/index.html.erb

<% @uniq_test = [] %>
    <% @galleries_order.each do |gallery| %>
        <% next if @uniq_test.include?(gallery.tag) %>
    <% @uniq_test << gallery.tag %>
<% end %>
<% @sorters = @sorters.map(&:downcase).sort! %>
<% @uniq_test = @uniq_test.map(&:downcase).sort! %>
<% @uniq_sorters = @uniq_test.zip(@sorters) %>
<div class="main">
    <div class="gallery-select-wrapper">
        <div class="sort-gallery-buttons animated slideInLeft text-center">
            <h2 id="recent"class="recent"><%= t"galleries.sorter.recent"%></h2>
    <% @uniq_sorters.each do |uniq, sorter| %>
        <% if sorter != nil %>
            <% str = "<h2 class='" + sorter + "'" + "id='"+ sorter + "'>"%>
            <%= str.html_safe + uniq + "</h2>".html_safe %>
        <% end %>
    <% end %>
        </div>
    </div>
</div>

コントローラー/ギャラリー.rb

def index
    @galleries = Gallery.all.order("created_at DESC")
    @galleries_order = Gallery.all.order("title ASC")
    @sorters = Gallery.uniq.pluck(:tag)
    gon.tags = Gallery.uniq.pluck(:tag)
end

en カテゴリは [country, theme, project, war] zh カテゴリは [主題,國家, 戰爭, 項目] <-- current (in en = theme, country, war, project) 私のカテゴリは [國家, 主題, 項目,戰爭] <-- ゴール (en と同じ)

一言で言えば、中国語の翻訳を英語のアルファベット順にしたいのです。

ここに画像の説明を入力

ここに画像の説明を入力

4

1 に答える 1

0

解決しました!要するに、globalize3 ::translation モデルで何かを並べ替えるには、私の場合は、中国語の文字を並べ替える方法を見つけることに対処しなければならないタイトルではなく、:idまたは私の場合は並べ替えるのが最善の方法です! :created_at用語がアルファベット順であるかどうかは特に気にしないので、同期していれば幸いです。

それと同じくらい簡単ですが、私は間違いなくそれを考えすぎていました!

ギャラリー/index.html

<% @array = [] %>
<% @test.each do |test| %>
    <% next if @array.include?(test.tag) %>
    <% @array << test.tag %>
<% end %>

<% @array_en = [] %>
<% @test.each do |test| %>
    <% next if @array_en.include?(test.tag_en) %>
    <% @array_en << test.tag_en %>
<% end %>

<% @combined_array = @array.zip(@array_en) %>

<div class="main">
    <div class="gallery-select-wrapper">
        <div class="sort-gallery-buttons animated slideInLeft text-center">
            <h2 id="recent"class="recent"><%= t"galleries.sorter.recent"%></h2>
    <% @combined_array.each do |uniq, sorter| %>
        <% if sorter != nil %>
            <% str = "<h2 class='" + sorter + "'" + "id='"+ sorter + "'>"%>
            <%= str.html_safe + uniq + "</h2>".html_safe %>
        <% end %>
    <% end %>
        </div>
    </div>
</div>

コントローラー/ギャラリー.rb

@test = Gallery.all.sort { |p1, p2| p1.created_at <=> p2.created_at }
于 2016-03-26T15:35:32.387 に答える