【问题标题】:How to use multiple models for tag_cloud?如何为 tag_cloud 使用多个模型?
【发布时间】:2015-04-27 21:27:49
【问题描述】:

_tags.html.erb

#Version 1 (Just lists out habits tags)
<% tag_cloud Habit.tag_counts, %w{s m l} do |tag, css_class| %>
  <%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>

#Version 2
<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
  <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>

我们如何在列出当前用户的习惯和目标、估值、量化指标的地方获得它? f.text_field :tag_list 在这四个模型中的每一个的_form中,它们也是数据库中的单独表。

我还在四个模型中添加了以下代码。以下是它寻找估值的方式:

助手

module ValuationsHelper
  include ActsAsTaggableOn::TagsHelper
end

控制器

class ValuationController < ApplicationController
  def tag_cloud
    @tags = Valuation.tag_counts_on(:tags)
  end
end

对于用户模型

  User.tag_counts_on(:tags)
  acts_as_tagger
  acts_as_taggable

我正在使用 acts-as-taggable-on gem,它是我从 railscasts 实现的。如果您需要任何进一步的代码或解释,请告诉我=]

【问题讨论】:

  • 你能贴出你的模型 has_many 属于 _to's
  • 如果 User.rb has_many 习惯和 Habit belongs_to User 可以使用 current_user.habits.tag_counts 标签云
  • 习惯、目标、估值和量化指标在您的数据库中是单独的表吗?
  • 那是正确的@Zippo9 抱歉我没有早点看到你的评论。

标签: ruby-on-rails ruby acts-as-taggable-on


【解决方案1】:

在用户模型中使用 gem 中的方法 with: acts_as_tagger 来设置特定于用户的标签。 act_as_taggable_on gem 文档示例:

    class User < ActiveRecord::Base
    acts_as_tagger
    end

    class Photo < ActiveRecord::Base
    acts_as_taggable_on :locations
    end

     @some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
     @some_user.owned_taggings
     @some_user.owned_tags

    @some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations, :skip_save => true) 

在您的情况下,您将需要设置一个包含以下 ID 的连接表:习惯、目标、评估和量化,然后您应该能够创建一个变量来调用该表或个人设置上的标签计数您视图中针对特定用户的每个习惯、目标、估值和量化指标。无论哪种方式,它都应该是这样的:

    @tags = YourModel.tag_counts_on(**context**)

更新尝试

    class User < ActiveRecord::Base
      acts_as_tagger
    end

    class Habit < ActiveRecord::Base
      # This goes for Valuation, Goal, and Quantified too.
      acts_as_taggable_on :combine_tags
    end

    class CombineTag < ActiveRecord::Base
      belongs_to :habit
      belongs_to :goal
      belongs_to :quantified
      belongs_to :valuation
    end

我尝试迁移这个:

    class CreateCombineTags < ActiveRecord::Migration
      def change
        create_table :combine_tags do |t|
          t.valuation_id :integer
          t.goal_id :integer
          t.quantified_id :integer
          t.habit_id :integer

          t.timestamps null: false
        end
      end
    end

但我得到了undefined method 'valuation_id' for #&lt;ActiveRecord:: 我不知道has_manybelongs_to 是否足以加入我假设是的模型。

那我该怎么处理@tags = YourModel.tag_counts_on(**context**)?上下文是什么意思?这是否包含在 _tags.html.erb 中?如果是这样,它看起来像这样:

<% @tags = CombineTag.tag_counts_on(**???**)
<% tag_cloud @tags.tag_counts, %w{s m l} do |tag, css_class| %>
  <%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>

【讨论】:

  • 嗨@Zippo9 我为这个问题添加了一个赏金,因为我无法得到你的答案。如果您能提供进一步的帮助,我将不胜感激:)
【解决方案2】:

创建一个名为 Tagalicious 的模型。

既然你想把它放在侧边栏中,就把它放在 application_controller 中:

before_action :tag_cloud

def tag_cloud
  @tags = Tagalicious.tag_counts_on(:tags)
end

然后在助手中:

module TagaliciousHelper
  include ActsAsTaggableOn::TagsHelper
end

然后在模型中:

class Tagalicious < ActiveRecord::Base
  belongs_to :habit
  belongs_to :goal
  belongs_to :quantified
  belongs_to :valuation
  acts_as_taggable
end

标签云:

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

希望这会澄清一些事情。我不知道如何使您的各种模型中的这条线&lt;%= f.text_field :tag_list %&gt; 指向Tagalicious 模型。

如果您愿意,我们可以在聊天中讨论这些内容,或者针对特定问题尝试其他问题。

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多