rails数组根据可用性排序

时间:2013-10-31 20:12:43

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

这是我的帖子控制器

  def refine id
    @sub_category_content = Post.where(sub_category_id: id).select('content')
    chained_array = []

    @sub_category_content.each do |content|
      form_chain = JSON.parse(content.content)
      chained_array << form_chain.values
    end

    @refine = chained_array.flatten.uniq

  end

这是我的应用程序控制器助手

def bypass_block refine, list
    refine = refine.include? list
    if refine
      'active'
    else
      'inactive'
    end
  end

这是我的观点

<% label.dropdown_lists.each do |list| %>
    <li>
    <% style = bypass_block(@refine, list.list_name) %>
    <a class="<%= style %>">
        <input type="checkbox" class="<%= style %>" />
        <%= list.list_name %>
    </a>
    </li>
<% end %>

@refine将通过数组["test","test123","tet22","abc","cds","sdd","cds"]

考虑我的列表名称将在循环tks, abc, ssld, cds, test

中传递以下值

所以在循环中我将输出为

实际输出

  1. TKS
  2. ABC
  3. SSLD
  4. CDS
  5. 测试
  6. 预期输出

    1. ABC
    2. CDS
    3. 测试
    4. TKS
    5. SSLD
    6. 我如何在这里进行这样的排序。

      修改 - 1

      .sort正在使用此

      <% @models.sort.each do |product_model| %>
      <% for_style = product_model.posts.any? ? 'active' : 'inactive' %>
      <li class="auto-view">
      <a class="<%= for_style %>">
      <input type="checkbox" class="<%= for_style %>" />
      <%= product_model.name %>
      </a>
      </li>
      <% end %>
      

      但是在上面的数组中它不起作用

      使用@refine = chained_array.flatten.sort.uniq

      enter image description here

      我认为只有颜色似乎不起作用

1 个答案:

答案 0 :(得分:1)

你能设置

吗?
@refine = chained_array.flatten.sort.uniq

在控制器中?

相关问题