如何为 tag_cloud 使用多个模型? [英] How to use multiple models for tag_cloud?
问题描述
_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 %>
我们如何才能在列出 current_user 的习惯和目标、估值、量化的地方获得它?f.text_field :tag_list
是这四个模型各自的_form,也是数据库中单独的表.
How can we get it where it list's out the current_user's habits AND goals, valuations, quantifieds? f.text_field :tag_list
is in the _form of each of these four models, which are also separate tables in the database.
我还向四个模型中的每一个添加了以下代码.以下是它寻找估值的方式:
I also added the below code to each of the four models. Here's is how it looks for valuations:
助手
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.如果您需要任何进一步的代码或解释,请告诉我 =]
I'm using the acts-as-taggable-on gem, which I implemented from railscasts. Please let me know if you need any further code or explanation =]
推荐答案
创建一个名为 Tagalicious
的模型.
Create a model called Tagalicious
.
因为你想把它放在侧边栏中,所以把它放在 application_controller 中:
Since you want it in the sidebar put this in the 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:
<% 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
模型.
Hopefully that will clarify some things. I can't figure out how to make this line in your various models <%= f.text_field :tag_list %>
point to the Tagalicious
model.
如果您愿意,我们可以在聊天中讨论这个问题,或者针对该特定问题尝试另一个问题.
This is something we can go over in chat if you want or maybe try another question for that specific problem.
这篇关于如何为 tag_cloud 使用多个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!