I have an application with a list of majors and each one is tagged with categories using the acts-as-taggable-on gem. I have a page where you can explore majors by category. So, you see the categories and grouped under the category is a list of the majors.
My categories_controller.rb
file:
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
You can see the duplication. This is compounded on the view template.
Here's part of the index.html.erb
page:
<!-- 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" %>
and so on for each category. I have tried @category = Major.tagged_with(params[:tag])
and many variations to that to no avail. This is my first time working with acts_as_taggable_on and although I've read the documentation over and over I can't quite figure this out.
I hope to extend this out throughout the application and so I want to figure it out now before I get a lot duplicate code. Thanks for sharing any ideas or suggestions!!
I am running a rails 3.2.11 app.
UPDATE
Here's how much better this is now:
My categories_controller.rb
file:
def index
@major_categories = ["creative arts", "social sciences", "science", ....]
end
My index.html.erb
page:
<% @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 %>