ruby on rails - Refactoring acts_as_taggable suggestions? -


i have application list of majors , each 1 tagged categories using acts-as-taggable-on gem. have page can explore majors category. so, see categories , grouped under category list of majors.

my categories_controller.rb file:

def index     @creative = major.tagged_with("creative arts")     @engineering = major.tagged_with("engineering , technology")     @mechanics = major.tagged_with("mechanics , 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 , marketing")     @administrative = major.tagged_with("administrative , clerical")     @services = major.tagged_with("services")     @tags = major.tag_counts end 

you can see duplication. compounded on view template.

here's part of index.html.erb page:

<!-- creative arts -->  <h2 class="major-categories-landing">creative arts</h2>  <% @creative.sample(10).each |creative| %> <%= link_to creative, class: 'span2 category-landing' %>     <%= 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 #{@creative.count} majors in 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 |ss| %>       <%= link_to ss, class: 'span2 category-landing' %>     <%= 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 #{@social_sciences.count} majors in category.", category_path("social sciences"), class: "view-category-show-page" %> 

and on each category. have tried @category = major.tagged_with(params[:tag]) , many variations no avail. first time working acts_as_taggable_on , although i've read documentation on , on can't quite figure out.

i hope extend out throughout application , want figure out before lot duplicate code. sharing ideas or suggestions!!

i running rails 3.2.11 app.

update
here's how better now:
categories_controller.rb file:

def index     @major_categories = ["creative arts", "social sciences", "science", ....] end 

my index.html.erb page:

<% @major_categories.each |c| %>     <!-- title , blue strip -->     <div class="category-strip">         <div class="container">             <h2 class="major-categories-landing"><%= c %></h2>         </div>     </div>      <!-- show each major in category -->      <div class="container">         <div class="row-fluid">              <% major.tagged_with(c).order('random()').limit(10).each |major| %>               <%= link_to major, class: 'span2 category-landing' %>                 <%= image_tag major.image(:similar), class: 'img-polaroid' %>                 <p class="category-landing-list-name"><%= major.name %></p>               <% end %>             <% end %>         </div>          <!-- link view majors -->         <div class="row-fluid">             <div class="view-all-category">                 <%= link_to "view #{major.tagged_with(c).count} majors in category.", category_path(c), class: "view-category-show-page" %>             </div>         </div>     </div> <% end %> 

i this:

# in categories_controller.rb def index   @categories = ["creative arts", "engineering , technology",                   "mechanics , 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 |major| %>   <%= link_to major, class: 'span2 category-landing' %>     <%= 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 #{major.tagged_with(category).count} majors in category.",              category_path(category), class: "view-category-show-page" %> 

btw: link each major invalid html. link (because a inline element) should not contain paragraph (because p box element). furthermore each link each category have same id, ids must unique in each html document.


Comments