How to use multiple models for tag_cloud?

99封情书 提交于 2019-12-01 19:07:27

Create a model called Tagalicious.

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

Then in the helper:

module TagaliciousHelper
  include ActsAsTaggableOn::TagsHelper
end

Then in the model:

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

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.

Zippo9

Use the method with in the gem: acts_as_tagger in the User model to set up tags specific to the user. Example for 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) 

In Your Case you will need to set up a join table that includes the ids of: Habits, Goals, Valuations and Quantifieds and then you should be able to create a variable to call the tag count on that table or individual set up each Habits, Goals, Valuations and Quantifieds in your views for a specific user. Either way it should look something like this:

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

UPDATE 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

I tried migrating this:

    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

but I got undefined method 'valuation_id' for #<ActiveRecord:: I don't know if has_many and belongs_to is enough to join the models I'm going to assume yes.

Then what do I do with @tags = YourModel.tag_counts_on(**context**)? What does context mean? Does this go in _tags.html.erb? If so would it look like this:

<% @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 %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!