gpt4 book ai didi

ruby-on-rails - 如何修复重复的标签?

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:24 25 4
gpt4 key购买 nike

用户可以提交习惯、目标、值(value)观和统计数据的标签。

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

enter image description here

关于这是为什么的任何想法?

页面 Controller

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

习惯.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

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

我试图只发布相关内容,但这是 gist 的。

最佳答案

你有没有试过取消注释行:

self.tag_list = nil

我猜你是从 this source 复制过来的或链接的计算器问题?在您的代码注释中,您询问了以下注释行的内容:

Clear the list so we don't get duplicate taggings

我挖掘了 sources并找到您正在调用的方法:

def set_owner_tag_list_on(owner, context, new_list)

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

虽然我真的不明白使用这个拥有的标签有什么意义,因为你为每个用户创造了新的习惯并且总是按用户过滤。据我所知,acts_as_taggable_on 的所有权功能仅在您拥有一个由多个用户标记的资源并且您想知道谁标记了什么时才有用。在您的情况下,每个人都有自己的可标记资源。

关于ruby-on-rails - 如何修复重复的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643598/

25 4 0