メジャーのリストを含むアプリケーションがあり、それぞれが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])
私はそれに対する多くのバリエーションを試しましたが、役に立ちませんでした。act_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 %>