0

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

1 回答 1

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,但ids 在每个 html 文档中必须是唯一的。

于 2013-09-07T17:32:27.423 回答