Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring acts_as_taggable suggestions?
    text
    copied!<p>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. </p> <p>My <code>categories_controller.rb</code> file: </p> <pre><code>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 </code></pre> <p>You can see the duplication. This is compounded on the view template. </p> <p>Here's part of the <code>index.html.erb</code> page: </p> <pre><code>&lt;!-- Creative Arts --&gt; &lt;h2 class="major-categories-landing"&gt;Creative Arts&lt;/h2&gt; &lt;% @creative.sample(10).each do |creative| %&gt; &lt;%= link_to creative, class: 'span2 category-landing' do %&gt; &lt;%= image_tag creative.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %&gt; &lt;p class="category-landing-list-name"&gt;&lt;%= creative.name %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= link_to "View all #{@creative.count} majors in this category.", category_path("creative arts"), class: "view-category-show-page" %&gt; &lt;!-- Social Sciences --&gt; &lt;h2 class="major-categories-landing"&gt;Social Sciences&lt;/h2&gt; &lt;% @social_sciences.sample(10).each do |ss| %&gt; &lt;%= link_to ss, class: 'span2 category-landing' do %&gt; &lt;%= image_tag ss.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %&gt; &lt;p class="category-landing-list-name"&gt;&lt;%= ss.name %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= link_to "View all #{@social_sciences.count} majors in this category.", category_path("social sciences"), class: "view-category-show-page" %&gt; </code></pre> <p>and so on for each category. I have tried <code>@category = Major.tagged_with(params[:tag])</code> 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. </p> <p>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!!</p> <p>I am running a rails 3.2.11 app. </p> <p><strong>UPDATE</strong><br> Here's how much better this is now:<br> My <code>categories_controller.rb</code> file: </p> <pre><code>def index @major_categories = ["creative arts", "social sciences", "science", ....] end </code></pre> <p>My <code>index.html.erb</code> page: </p> <pre><code>&lt;% @major_categories.each do |c| %&gt; &lt;!-- Title and blue strip --&gt; &lt;div class="category-strip"&gt; &lt;div class="container"&gt; &lt;h2 class="major-categories-landing"&gt;&lt;%= c %&gt;&lt;/h2&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- Show Each Major in this Category --&gt; &lt;div class="container"&gt; &lt;div class="row-fluid"&gt; &lt;% Major.tagged_with(c).order('RANDOM()').limit(10).each do |major| %&gt; &lt;%= link_to major, class: 'span2 category-landing' do %&gt; &lt;%= image_tag major.image(:similar), class: 'img-polaroid' %&gt; &lt;p class="category-landing-list-name"&gt;&lt;%= major.name %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;!-- Link to View All Majors --&gt; &lt;div class="row-fluid"&gt; &lt;div class="view-all-category"&gt; &lt;%= link_to "View all #{Major.tagged_with(c).count} majors in this category.", category_path(c), class: "view-category-show-page" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload