English 中文(简体)
Rails category navigation with tags
原标题:

I want to create a navigation system for my web application where there are several main categories for articles and several sub tags for each category.

For example, in category "Writing" there might be subcategories like "Essays," "Poetry" and "Fiction."

Each article would have a list of tags. If someone clicks the tag "Italy" on the fiction category show page I wouldn t want articles with the same tag of "Italy" from other categories like Essays or Poetry to appear on that page.

When a tag is clicked, the show page would be a category show page with a tag filter for all the articles with that particular tag in that category. It would not be a Tag show page.

Also, I d like a sidebar with related tags. These would be tags that are in the same articles as those displayed on the category show page in descending order. I got related tags to work on the Tag page itself but not with the category constraint.

So far, I can get category pages and tag pages to work seperately, but I d like to integrate the two.

I m currently using Acts as Taggable on Steroids for the Tags and Acts as Tree for the categories.

Any ideas?

最佳回答

Matt, thanks a lot! This seems a lot simpler than what I had originally thought of.

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.

After a bit of trial and error I got my code to work. It seems messy though. Any suggestions on simplification?

BTW some of these methods are defined through AATOS

In categories view#show:

<% for article in @articles %>

  <%= link_to article.name, article %>
  <% unless article.tag_list.empty? %>
  <p class="tags">
   Tags:
   <%= render :partial  => article.tags %>
   </p>
  <% end %>

And for the tag/navigation sidebar I have:

<% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>
<%= link_to tag, category_path(:filter => tag.name), :class => css_class %>
<% end %>

The category model, (not ideal but it works)

class Category < ActiveRecord::Base
acts_as_tree
has_many :articles
has_many :child_articles, :through => :children, :source => :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

end

Finally the ugly controller code:

class CategoriesController < 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 => ["articles.id IN (?)", @category.family])
 end
 @tags = 
 if (params[:filter]).nil?
 Article.tag_counts :conditions => ["articles.id IN (?)", @category.family]
 else
 Article.find_related_tags(params[:filter],
 :conditions => ["tags.id IN (?)",  @category.family.map {|a| a.tags}.flatten.uniq])
 end

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.

问题回答

I think you are mixing two concepts here, a least in the question, perhaps not in your application. You have a category list modeled with acts_as_tree. That supports subcategories. The tags should not belong to subcategories. You want to filter by tag and category, more or less. You should be able to write simple queries that return lists of articles, such as:

@articles = Article.find(:all, 
        :include => "categories"
        :conditions => ["(category_id = ? OR categories.parent_id = ?) AND tag_id = ?",
                         category_id, category_id, tag_id])

One more complex approach that I have used to do this is by doing faceted navigation with Solr. It supports combining these filters with full text search.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签