如何为tag_cloud使用多个模型?

_tags.html.erb

#Version 1 (Just lists out habits tags)    #Version 2   :tag, :id => tag.name }, :class => css_class %>  

我们怎样才能得到它列出current_user的习惯和目标,估值和量化? 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实现的。 如果您需要任何进一步的代码或解释,请告诉我=]

创建一个名为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 %> 

希望这会澄清一些事情。 我无法弄清楚如何在各种模型中使这一行<%= f.text_field :tag_list %>指向Tagalicious模型。

这是我们可以在聊天中查看的内容,或者可能针对该特定问题尝试其他问题。

在用户模型中使用gem: acts_as_tagger中的方法设置特定于用户的标记。 acts_as_taggable_on gem docs的示例:

  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**) 

更新ATTEMPT

  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 #得到了undefined method 'valuation_id' for #我不知道has_manybelongs_to是否足以加入模型我将假设是。

那么我怎么处理@tags = YourModel.tag_counts_on(**context**) ? 语境是什么意思? 这是_tags.html.erb吗? 如果是这样,它看起来像这样:

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