我有一个包含专业列表的应用程序,每个专业都使用acts-as-taggable-on gem 标记有类别。我有一个页面,您可以在其中按类别探索专业。因此,您会看到类别,并且在类别下分组的是专业列表。
我的categories_controller.rb
文件:
def index
@creative = Major.tagged_with("creative arts")
@engineering = Major.tagged_with("engineering and technology")
@mechanics = Major.tagged_with("mechanics and construction")
@transportation = Major.tagged_with("transportation")
@science = Major.tagged_with("science")
@math = Major.tagged_with("math")
@resources = Major.tagged_with("natural resources")
@healthcare = Major.tagged_with("health care")
@social_sciences = Major.tagged_with("social sciences")
@education = Major.tagged_with("education")
@law = Major.tagged_with("law")
@management = Major.tagged_with("management and marketing")
@administrative = Major.tagged_with("administrative and clerical")
@services = Major.tagged_with("services")
@tags = Major.tag_counts
end
你可以看到重复。这是在视图模板上复合的。
这是index.html.erb
页面的一部分:
<!-- Creative Arts -->
<h2 class="major-categories-landing">Creative Arts</h2>
<% @creative.sample(10).each do |creative| %>
<%= link_to creative, class: 'span2 category-landing' do %>
<%= image_tag creative.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= creative.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{@creative.count} majors in this category.", category_path("creative arts"), class: "view-category-show-page" %>
<!-- Social Sciences -->
<h2 class="major-categories-landing">Social Sciences</h2>
<% @social_sciences.sample(10).each do |ss| %>
<%= link_to ss, class: 'span2 category-landing' do %>
<%= image_tag ss.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= ss.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{@social_sciences.count} majors in this category.", category_path("social sciences"), class: "view-category-show-page" %>
依此类推每个类别。我已经尝试过@category = Major.tagged_with(params[:tag])
很多变化,但无济于事。这是我第一次使用acts_as_taggable_on,虽然我一遍又一遍地阅读了文档,但我还是不太明白这一点。
我希望在整个应用程序中扩展它,所以我想在得到大量重复代码之前弄清楚它。感谢您分享任何想法或建议!!
我正在运行一个 rails 3.2.11 应用程序。
更新
这现在好多了:
我的categories_controller.rb
文件:
def index
@major_categories = ["creative arts", "social sciences", "science", ....]
end
我的index.html.erb
页面:
<% @major_categories.each do |c| %>
<!-- Title and blue strip -->
<div class="category-strip">
<div class="container">
<h2 class="major-categories-landing"><%= c %></h2>
</div>
</div>
<!-- Show Each Major in this Category -->
<div class="container">
<div class="row-fluid">
<% Major.tagged_with(c).order('RANDOM()').limit(10).each do |major| %>
<%= link_to major, class: 'span2 category-landing' do %>
<%= image_tag major.image(:similar), class: 'img-polaroid' %>
<p class="category-landing-list-name"><%= major.name %></p>
<% end %>
<% end %>
</div>
<!-- Link to View All Majors -->
<div class="row-fluid">
<div class="view-all-category">
<%= link_to "View all #{Major.tagged_with(c).count} majors in this category.", category_path(c), class: "view-category-show-page" %>
</div>
</div>
</div>
<% end %>