How to Use Multiple Models for Tag_Cloud

How to use multiple models for tag_cloud?

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.

How to make a multi model tag_cloud with a join table?

In my opinion, You could use polimorphing. Please, see Active Record Associations

In Your case, Model could be next:

class Tag < ActiveRecord::Base
belongs_to :taggable, polymorphic: true
....

class Habit < ActiveRecord::Base
has_many :tags, as: :taggable
....

class Goal < ActiveRecord::Base
has_many :tags, as: :taggable
....

And in migrations:

create_table :tags , force: true do |t|
t.references :taggable, polymorphic: true, index: true
t.timestamps null: false
end

After this You can:

@tags = Tag.include(:taggable)
@tags.each do |tag|
type = tag.taggable_type # string, some of 'habit', 'goal' etc
id = tag.taggable_id # id of 'habit', 'goal' etc
end

Multiple Tag Fields on One Model in Django, or ManyToMany Fields?

Negatives

  • Django tag fields don't have slug fields so you will manually have to slugify the tag.name if you want to use them in Urls

  • You can't (without subclassing django-taggings models) add any other fields to a genre or book tag so you won't easily be able to describe the genre or tag.

  • Regarding useability: If you are using django-tagging you will more then likely be using a TagField to enter the tags (be them genres or booktags). The problem with this is that you need to remember exactly what tags you have used before, as opposed to a foreignkey or manytomanyfield where you are selecting from a list.

Positives

  • Django tagging has a nice API for querying tags and tagged items

django-taggit is another tagging application that is widely used and still developed and overcomes some of the above limitations

Creating a tag cloud from a list in Django?

There are many ways you could do this. Personally I would manipulate the list of tags into this format in you view:

tags = [
{ 'tag': 'django', 'size': 10 },
{ 'tag': 'python', 'size': 8 },
{ 'tag': 'Australia', 'size': 1 },
{ 'tag': 'coffee', 'size': 6 },
{ 'tag': 'pycon', 'size': 3 },
{ 'tag': 'html', 'size': 9 },
]

In your template:

<div class="tag-cloud">
{% for t in tags %}
<a href="/blog/tag/{{ t.tag }}/" class="size-{{ t.size }}">{{ t.tag }}</a>
{% endfor %}
</div>

In your CSS:

.tag-cloud a.size-1 { font-size: 1.1em }
.tag-cloud a.size-2 { font-size: 1.2em }
.tag-cloud a.size-3 { font-size: 1.3em }
.tag-cloud a.size-4 { font-size: 1.4em }
.tag-cloud a.size-5 { font-size: 1.5em }
.tag-cloud a.size-6 { font-size: 1.6em }
.tag-cloud a.size-7 { font-size: 1.7em }
.tag-cloud a.size-8 { font-size: 1.8em }
.tag-cloud a.size-9 { font-size: 1.9em }
.tag-cloud a.size-10 { font-size: 2em }

Django filtering by multiple tags in many to many ORM object

qsts_pks = QueryStringTag.objects.filter(tag__pk__in=['12', '14', '15']).values_list('id', flat=True)
queries = QueryString.objects.filter(qsquerystring__pk__in=qsts_pks)

Relevant docs here and here

Creating a tag cloud out of taglink?

I have now able to count every tag in every post with following:

Dictionary<string, int> tagLista = new Dictionary<string, int>();

foreach (Post post in Model)
{
foreach (Tag tag in post.Tags)
{
if (!tagLista.ContainsKey(tag.Name))
{
tagLista[tag.Name] = 1;
}
else
{
tagLista[tag.Name] += 1;
}
}
}
//Print out diff. sizes depending on count
foreach (KeyValuePair<string, int> pair in tagLista)
{

if (pair.Value <= 2)
{
<a class="minSize" href="@Href("~/Posts/Tags/" + pair.Key)">@pair.Key</a>
}

if (pair.Value > 2 && pair.Value <= 4)
{
<a class="medSize" href="@Href("~/Posts/Tags/" + pair.Key)">@pair.Key</a>
}
if (pair.Value > 4 && pair.Value >= 6)
{
<a class="maxSize" href="@Href("~/Posts/Tags/" + pair.Key)">@pair.Key</a>
}
}

The problem here is now that it only shows the tags of all posts on the current page, and not all tags from the database. How should I do instead? On the top of this view(Index) I use:

@using Blog.Models;
@model IEnumerable<Post>

Thanks for any help!

How to display a tag cloud from Acts as Taggable On on an index page in Ruby on Rails 3.2.7?

OK, after hacking about a bit, I think I've found a solution, in case anyone else wondering how to do this happens to stumble across this question.

However, please be aware that I am very much a beginner at RoR, so this is probably not the best solution - if I'm doing anything wrong, or if you have a better solution, feel free to let me know!

Add this code in your view to display the list of tags for a particular model in order:

@tags = ActsAsTaggableOn::Tag.all(:order=>'name')

<% if @tags.count > 0 %>
<ul>
<% @tags.each do |tag| %>
<li><%= link_to tag.name, tagged_url(:tag => tag.name) %></li>
<% end %>
</ul>
<% else %>
<p>There are no tags on the system.</p>
<% end %>

This results in a very basic display and, due to my inexperience I advise using this approach with caution - I'm sure it's not the best, or even the "safest", method, so beware!



Related Topics



Leave a reply



Submit