-->

How to use multiple models for tag_cloud?

2020-04-07 18:57发布

问题:

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

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:

Helper

module ValuationsHelper
  include ActsAsTaggableOn::TagsHelper
end

Controller

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

and for the User Model

  User.tag_counts_on(:tags)
  acts_as_tagger
  acts_as_taggable

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 =]

回答1:

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.



回答2:

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