【发布时间】:2017-01-16 19:18:18
【问题描述】:
在我的应用程序中,我有一个提供商列表,每个提供商都有类别。我的模型如下所示:
class Categorization < ApplicationRecord
belongs_to :category
belongs_to :provider
end
class Category < ApplicationRecord
has_many :categorizations
has_many :providers, through: :categorizations
accepts_nested_attributes_for :categorizations
end
class Provider < ApplicationRecord
has_many :categorizations
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categorizations
end
在一个视图中,我有 2 个ul 列表。第一个是提供者列表,第二个是类别列表。它看起来像:
<ul class="nav nav-justified nav-carousel upper-thumbs">
<% Provider.order(created_at: :asc).take(6).each_with_index do |provider, index| %>
<li id="<%= provider.name %>_<%= index + 1 %>" class="btn-up"><a data-href="#"><%= provider.name %></a></li>
<% end %>
</ul>
<ul id="parent" class="nav nav-justified nav-carousel left-thumbs">
<% Category.order(created_at: :asc).take(7).each_with_index do |category, index| %>
<li id="aside_<%= index + 1 %>" class="btn-aside"><i class="category_image" style="background: url(<%= category.cat_image %>) no-repeat;"></i><a data-href="#"><%= category.name %></a></li>
<% end %>
</ul>
当我没有实现 rails 时,当我单击提供程序(它也被硬编码为类别)时,我可以看到,类别已被过滤。是js制作的:
var $btns = $('.btn-up').click(function () {
if(this.id == 'a') {
$('#parent > li#aside_1').fadeIn(450);
$('#parent > li').not($('#parent > li#aside_1')).hide();
} else {
var $el = $('.' + this.id).fadeIn(450);
$('#parent > li').not($el).hide();
}
$btns.removeClass('active');
$(this).addClass('active');
})
所以,问题是如何使过滤器工作,所以当我点击一个提供者时,它只会显示属于该提供者的类别?谢谢。
【问题讨论】:
标签: jquery ruby-on-rails filtering