How to use multiple models for tag_cloud?

_tags.html.erb

#Version 1 (Just lists out habits tags) <% tag_cloud Habit.tag_counts, %w{sml} 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 lists the habits and goals, scores, quantifications of current_user ? 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 code below to each of the four models. Here's how he looks for ratings:

Assistant

 module ValuationsHelper include ActsAsTaggableOn::TagsHelper end 

controller

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

and for user model

  User.tag_counts_on(:tags) acts_as_tagger acts_as_taggable 

I am using the act-as-taggable-on gem that I implemented from railscasts , Please let me know if you need further code or clarification =]

+4
ruby ruby-on-rails acts-as-taggable-on
source share
2 answers

Create a model called Tagalicious .

Since you want this in the sidebar to put this in application_controller:

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

Then in the assistant:

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

Hope this clarifies some things. I cannot figure out how to make this line in your various models <%= f.text_field :tag_list %> pointer to a Tagalicious model.

This is something we can experience in the chat if you want, or maybe try another question for this particular problem.

+1
source share

Use the c method in gem: act_as_tagger in the User model to customize user-specific tags. Example for actions_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 connection table, which includes identifiers: Habits, Goals, Valuations and Quantifieds, and then you can create a variable to call up the tag counter in this table or to customize each skill, goals, ratings and quantitative estimates in your submissions for a specific user. In any case, 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 to migrate:

  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 an undefined method 'valuation_id' for #<ActiveRecord:: I don’t know if has_many and belongs_to enough to join the models that I am going to accept yes.

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

 <% @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 %> 
+3
source share

All Articles