重构acts_as_taggable建议?

时间:2013-07-11 18:30:02

标签: ruby-on-rails ruby-on-rails-3 refactoring acts-as-taggable-on

我有一个包含专业列表的应用程序,每个专业都使用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 app。

更新
这是现在的好多了:
我的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 %>

1 个答案:

答案 0 :(得分: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是一个框元素)。此外,每个类别的每个链接都具有相同的id,但id在每个html文档中必须是唯一的。