【问题标题】:dynamic tag clouds in Ruby on RailsRuby on Rails 中的动态标签云
【发布时间】:2014-06-17 13:38:48
【问题描述】:

我使用this 教程从头开始在 Ruby on Rails 中实现标签云。

当您单击我的照片博客上的标签时,例如东京,所有带有东京标签的照片都会列出。我现在想添加 2 件事来使我的标签云更加动态,以便您可以缩小列表范围:

  • 标签云应该动态更新,以便它现在显示标签与剩余照片相关联,东京子集(字体大小相对于该子集缩放)。
  • 当您单击此子集标签云中的标签时,例如 2008,我希望列出所有带有 Tokyo 2008(而不是所有张带有2008标签的照片),最好是无限

我是 Ruby 新手,无论我尝试过什么,似乎都无法完成其中任何一个(此处无法列出)。

以下是相关代码:

照片.rb:

class Photo < ActiveRecord::Base
belongs_to :photo
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
accepts_nested_attributes_for :tags

def self.tagged_with(name)
    Tag.find_by_name!(name).photos
end

def self.tag_counts
    Tag.select("tags.*, count(taggings.tag_id) as count").
    joins(:taggings).group("taggings.tag_id")
end

tag.rb:

class Tag < ActiveRecord::Base
    has_many :taggings
    has_many :photos, through: :taggings
end

标记.rb:

class Tagging < ActiveRecord::Base
    belongs_to :tag
    belongs_to :photo
end

application_helper.rb:

module ApplicationHelper
def tag_cloud(tags, classes)
    max = 0
    tags.each do |t|
        if t.count.to_i > max
            max = t.count.to_i
        end
    end
    tags.each do |tag|
        index = tag.count.to_f / max * (classes.size - 1)
        yield(tag, classes[index.round])
    end
end
end

photos_controller.rb:

class PhotosController < ApplicationController

  def index
      if params[:tag]
      @photos = Photo.tagged_with(params[:tag])
      else
      @photos = Photo.order("created_at desc").limit(8)
      end
  end

  private
  def set_photo
      @photo = Photo.find(params[:id])
  end

  def photo_params
      params.require(:photo).permit(:name, :description, :picture, :tag_list, tags_attributes: :name)
  end
  end

index.html.erb:

<div id="tag_cloud">
  <% tag_cloud Photo.tag_counts, %w[xxs xs s m l xl xxl] do |tag, css_class| %>
  <%= link_to tag.name, tag_path(tag.name), class: css_class %>
  <% end %>
</div>

photos.css.scss:

#tag_cloud {
  width: 400px;
  line-height: 1.6em;
  .xxs { font-size: 0.8em; COLOR: #c3c4c4; }
  .xs { font-size: 1.0em; COLOR: #b9bdbd; }
  .s { font-size: 1.2em; COLOR: #b0b5b5; }
  .m { font-size: 1.4em; COLOR: #9da6a6; }
  .l { font-size: 1.6em; COLOR: #8a9797; }
  .xl { font-size: 1.8em; COLOR: #809090; }
  .xxl { font-size: 2.0em; COLOR: #778888; }
}

任何帮助将不胜感激!

【问题讨论】:

    标签: ruby-on-rails ruby tags tag-cloud


    【解决方案1】:

    如果我理解正确,您希望能够选择带有多个标签之一的照片。你需要改变你的功能:

    def self.tagged_with(*names)
        joins(:tags).where(tags: { name: names })
    end
    

    以及参数名称:

    def index
      if params[:tags]
        @photos = Photo.tagged_with(params[:tags])
      else
        @photos = Photo.order("created_at desc").limit(8)
      end
    end
    

    在视图中,当你定义方法current_tags

    <div id="tag_cloud">
      <% tag_cloud Photo.tag_counts, %w[xxs xs s m l xl xxl] do |tag, css_class| %>
      <%= link_to tag.name, tag_path([tag.name, *current_tags].uniq), class: css_class %>
      <% end %>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2012-07-05
      • 1970-01-01
      • 2022-01-17
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多