Rails category category navigation with tags

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

For example, in the Writing category, there may be subcategories such as Essays, Poetry, and Fiction.

Each article will have a list of tags. If someone clicks the Italy tag on the fiction category page, I don’t want the page to display the same Italy tag from other categories, such as Essays or Poetry.

When a tag is clicked on the show page, a category page will be displayed with a tag filter for all articles with this specific tag in this category. This will not be a tag display page.

Also, I need a sidebar with related tags. These are tags that are in the same articles as those that appear on the category display page in descending order. I have related tags to work on the tag page itself, but not with a category restriction.

Until now, I could get category and tag pages to work separately, but I would like to integrate them.

I am currently using Acts as Taggable for steroids for tags and Acts as Tree for categories.

Any ideas?

+3
source share
2

, ! , .

, . , , . , , , .

. , . ?

, AATOS

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 %>

tag/navigation :

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

( , )

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

, :

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

, . , . .

0

, , , , , . , act_as_tree. . . , . , , :

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

, , - Solr. .

+1

Source: https://habr.com/ru/post/1723713/


All Articles