Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Matt, thanks a lot! This seems a lot simpler than what I had originally thought of.</p> <p>However, my goal was to include subcategories in the navigation. The idea is that the parent categories would have everything in the subcategories and that if you clicked the sub categories it would narrow it down. I think that I'll check out Solr thought and see if it makes my code simpler.</p> <p>After a bit of trial and error I got my code to work. It seems messy though. Any suggestions on simplification?</p> <p>BTW some of these methods are defined through AATOS</p> <p>In categories view#show:</p> <pre><code>&lt;% for article in @articles %&gt; &lt;%= link_to article.name, article %&gt; &lt;% unless article.tag_list.empty? %&gt; &lt;p class="tags"&gt; Tags: &lt;%= render :partial =&gt; article.tags %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>And for the tag/navigation sidebar I have:</p> <pre><code>&lt;% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %&gt; &lt;%= link_to tag, category_path(:filter =&gt; tag.name), :class =&gt; css_class %&gt; &lt;% end %&gt; </code></pre> <p>The category model, (not ideal but it works)</p> <pre><code>class Category &lt; ActiveRecord::Base acts_as_tree has_many :articles has_many :child_articles, :through =&gt; :children, :source =&gt; :articles def grand_articles children.map {|c| c.child_articles}.flatten.uniq end def to_param name end def family child_articles + articles + grand_articles end </code></pre> <p>end</p> <p>Finally the ugly controller code:</p> <pre><code>class CategoriesController &lt; ApplicationController def show @category = Category.find_by_name(params[:id]) if (params[:filter]).nil? @articles = @category.family else @articles = Article.find_tagged_with(params[:filter], :conditions =&gt; ["articles.id IN (?)", @category.family]) end @tags = if (params[:filter]).nil? Article.tag_counts :conditions =&gt; ["articles.id IN (?)", @category.family] else Article.find_related_tags(params[:filter], :conditions =&gt; ["tags.id IN (?)", @category.family.map {|a| a.tags}.flatten.uniq]) end </code></pre> <p>It works but again it's ugly and brittle code. I'm sure I've committed some terrible sins here. Any suggestions for cleaning it up is appreciated.</p>
 

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