我有一個專業列表的應用程序,每個人都用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