我有一个专业列表的应用程序,每个人都用gem上的acts-as-taggable-tags来标记类别。我有一个页面,您可以按类别浏览专业。所以,你看到的类别和分类下的类别是专业列表。重构acts_as_taggable建议?
我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应用程序。
UPDATE
下面是如何更好,这是现在:
我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 %>
谢谢!阵列完美运作!这正是我想要做的 - 它都是循环的,没有更多的重复代码。真的,谢谢你!很好的抓住了BTW的东西。我也解决了这些问题。 – lflores