0

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

回答

2

我會做這樣的事情:

# in categories_controller.rb 
def index 
    @categories = ["creative arts", "engineering and technology", 
       "mechanics and construction", ...] 
end 

# in index.html.erb 
<%= render partial: "category", collection: @categories %> 

# in _category.html.erb 
<h2 class="major-categories-landing"><%= category.titleize %></h2> 

<% Major.tagged_with(category).order('rand()').limit(10).each do |major| %> 
    <%= link_to major, class: 'span2 category-landing' do %> 
    <%= image_tag major.image(:similar), class: 'img-polaroid', 
             id: 'category-landing-list' %> 
    <p class="category-landing-list-name"><%= major.name %></p> 
    <% end %> 
<% end %> 

<%= link_to "View all #{Major.tagged_with(category).count} majors in this category.", 
      category_path(category), class: "view-category-show-page" %> 

順便說一句:每個專業的鏈接是無效的HTML。一個鏈接(因爲a它是一個內聯元素)不應該包含一個段落(因爲p是一個box元素)。此外,每個類別的每個鏈接將具有相同的id,但id必須在每個html文檔中都是唯一的。

+0

謝謝!陣列完美運作!這正是我想要做的 - 它都是循環的,沒有更多的重複代碼。真的,謝謝你!很好的抓住了BTW的東西。我也解決了這些問題。 – lflores