如何修复重复的标签? [英] How to fix duplicate taggings?

查看:57
本文介绍了如何修复重复的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以提交习惯、目标、价值观和统计数据的标签.

当用户点击 tag_cloud 中的标签时,他会被重定向到包含该标签所有实例的主页,但由于某种原因,属于习惯的实例会重复.>

对为什么会这样有任何想法吗?

pages_controller

@habits = current_user.habits.tagged_with(params[:tag])

habit.rb

班级习惯

views/home.html.erb 中:<%= render @habits %>routes.rb 中:root 'pages#home'.

我尝试只发布相关内容,但这里是 要点 它.

解决方案

您是否尝试过取消注释第 1 行:

self.tag_list = nil

我猜你是从这个来源复制过来的,或者链接的stackoverflow 问题?在您的代码注释中,您询问以下注释行是关于什么的:

<块引用>

清除列表,这样我们就不会得到重复的标签

我仔细研究了 sources 并找到您正在调用的方法:

def set_owner_tag_list_on(owner, context, new_list)

因为最后一个参数被称为 new_list,我猜你交给 set_owner_tag_list_on 方法的旧列表将再次设置相同的标签.因此,没有所有者的旧 tag_list 在那里设置为零,因为 tag_list 似乎只包含没有所有者的标签(根据 docs)

尽管毕竟我真的不明白使用这种自有标签的意义,因为您为每个用户创造了新习惯并始终按用户进行过滤.据我所知,acts_as_taggable_on 的所有权功能仅在您拥有一个被多个用户标记的资源并且您想知道谁标记了什么时才有用.就您而言,每个人都有自己的可标记资源.

Users can submit tags for Habits, Goals, Values, and Stats.

When a user clicks on a tag in the tag_cloud he is redirected to the home page with all the instances of that tag, but for some reason the instances that fall under Habits are duplicating.

Any ideas on why this is?

pages_controller

@habits = current_user.habits.tagged_with(params[:tag])

habit.rb

class Habit < ActiveRecord::Base
  belongs_to :user
  acts_as_taggable
  before_save :set_tag_owner

  def set_tag_owner 
    # Set the owner of some tags based on the current tag_list
    set_owner_tag_list_on(self.user, :tags, self.tag_list)
    # Clear the list so we don't get duplicate taggings (hmmm what does this mean? I copied this code & comment from somewhere else)
    # self.tag_list = nil
  end

In views/home.html.erb: <%= render @habits %> in routes.rb: root 'pages#home'.

I tried to post only the relevant stuff, but here's the gist of it.

解决方案

Have you tried uncommenting th line:

self.tag_list = nil

I guess you copied it over from this source or the linked stackoverflow question ? In your code comments you asked what the following comment line is about:

Clear the list so we don't get duplicate taggings

I digged through the sources and found the method you are calling:

def set_owner_tag_list_on(owner, context, new_list)

So as the last parameter is called new_list, i guess the old list, that you hand in to the set_owner_tag_list_on method, is would set the same tags again. Therefore the old tag_list without the owner is set to nil there, as tag_list only seems to contain the tags without owner (according to the docs)

Although I don't really see the point of using this owned taggings after all, since you create new habits for every user and always filter by user. As far as I can tell the ownership feature of acts_as_taggable_on is only useful when you have one recource that is tagged by multiple users and you want to know who tagged what. In your case everyone has his own taggable resources.

这篇关于如何修复重复的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆