【问题标题】:Weird behaviors on acts-as-taggable-on gem of not clearing taggings不清除标记的行为作为可标记宝石的奇怪行为
【发布时间】:2015-08-21 07:06:46
【问题描述】:

我有一个名为 Recipe 的模型,它使用 acts-as-taggable-on gem 将标签添加到食谱中。奇怪的行为是当我通过控制器编辑和更新配方时,配方标签被添加,而不是更新到正确的标签。有人报告了类似的Github issue,但没有任何人回复。我作为可标记的 gem 版本是 3.2.6。

例如,我的食谱有三个标签:fruitfruitvegetablesfruit 标签不相关,因此我在f.tag_list 输入字段中删除了这两个fruit 标签,只包含vegetables

Processing by Users::RecipesController#update as JS
Parameters: {"utf8"=>"✓", "recipe"=>{"image_ids"=>"46746", "cover_image_id"=>"46746",
"name"=>"Cauliflower", "description"=>"this is the description", "preparation_time_hr"=>"0",
"preparation_time_min"=>"50", "servings"=>"4", 
"category_ids"=>"", "cuisine_ids"=>"", "tag_list"=>"vegetables", "ingredients"=>....}

用户/recipes_controller.rb

def update
  if @recipe.update_attributes(recipe_params)
    if params[:publish_recipe] && params[:publish_recipe] == "true"
      update_status!(:publish!)
    end
    redirect_to user_recipes_url
  else
    respond_to do |format|
      format.html{ render "edit" }
      format.js { render partial: "shared/flash_message", locals: { flash_type: "error", flash_message: "#{@recipe.errors.full_messages.to_sentence}" } }
    end
  end
end

recipe_params 包含 tag_list 作为允许的参数。

def recipe_params
  params.required(:recipe).permit(:name, :category_ids, :cuisine_ids, :tag_list, :preparation_time_hr,
  :cover_image_id, :preparation_time_min, :servings, :description, :image_ids, :commentable,
  ingredients_attributes: [:id, :name, :_destroy],
  instructions_attributes: [:id, :text, :_destroy, image_attributes: [:id, :picture, :_destroy]]
) rescue {}
end

但最终旧标签没有被删除,新标签已添加到列表中:fruitfruitvegetablesvegetables

另一个奇怪的事情是,当我尝试查看配方的 tag_list 时,它是一个空数组。尝试在 Rails 控制台中编辑 tag_list,它添加了另一个标记,但是当我执行 r.reload tag_list 时,它仍然是一个空数组。现在菜谱和标签的关系是这样的:

r = Recipe.find(20980)
[56] pry(main)> r.tag_list
 ActsAsTaggableOn::Tag Load (14.9ms)  SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 20980 AND `taggings`.`taggable_type` = 'Recipe' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
=> []
[57] pry(main)> r.tags
=> [#<ActsAsTaggableOn::Tag id: 2, name: "fruit", taggings_count: 1138, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:47:20", updated_at: "2015-07-25 15:47:53">,
 #<ActsAsTaggableOn::Tag id: 2, name: "fruit", taggings_count: 1138, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:47:20", updated_at: "2015-07-25 15:47:53">,
 #<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">,
 #<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">,
 #<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">]
[58] pry(main)> r.taggings
  ActsAsTaggableOn::Tagging Load (35.8ms)  SELECT `taggings`.* FROM `taggings`  WHERE `taggings`.`taggable_id` = 20980 AND `taggings`.`taggable_type` = 'Recipe'
=> [#<ActsAsTaggableOn::Tagging id: 20408, tag_id: 2, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 03:56:13">,
 #<ActsAsTaggableOn::Tagging id: 20409, tag_id: 2, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 03:56:14">,
 #<ActsAsTaggableOn::Tagging id: 20410, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:01:36">,
 #<ActsAsTaggableOn::Tagging id: 20411, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:47:38">,
 #<ActsAsTaggableOn::Tagging id: 20412, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:53:38">]

tag_list 数据库查询也很奇怪,为什么查询包含这个taggings.tagger_id IS NULL

我还是这个 gem 的新手,知道如何使用 gem 方法正确更新标记吗?我希望我可以避免使用我自己的代码更新标签以避免进一步的问题。注意到我的Recipe模型是paper_trailgem版本的,希望和这个问题没有关系。

【问题讨论】:

  • 你能发布你的recipe_params吗?
  • @Pavan 在问题中添加了recipe_params,谢谢

标签: ruby-on-rails ruby ruby-on-rails-4 acts-as-taggable-on


【解决方案1】:

由于互联网上没有解决方案,我只在配方上使用这个可标记,现在我手动调用一个方法来在模型保存后重新更新标签。

def update_tags(tag_list)
  self.taggings.destroy_all
  list = tag_list.split(",")
  list.each do |t|
    tag = Tag.find_or_create_by(name: t)
    self.taggings.create(tag_id: tag.id, context: "tags")
  end
  return self.taggings
end

我仍然认为这个问题有更好的解决方案,我愿意接受替代方案作为最佳答案。

【讨论】:

    【解决方案2】:

    我在 act-as-taggable-on 文档中看不到任何关于当 tags_list 作为参数进入 update_attributes 方法时会发生什么的任何内容,但在您的情况下,它似乎添加了标签而不删除现有的。我确实在文档中注意到 tag_list 可以用替换现有标签的= 分配:

    您还可以通过直接分配添加和删除标签。请注意,这将删除现有标签,因此请谨慎使用。

    @user.tag_list = "awesome, slick, hefty"
    @user.save
    

    所以你可以在你的更新方法中做到这一点

    def update
      if @recipe.update_attributes(recipe_params)
        @recipe.tag_list = recipe_params["tag_list"]
        @recipe.save
        ...
      else
        ...
      end
    end
    

    【讨论】:

    • 我确实在我的控制台中分配给tag_list 来测试这一点,但仍然添加了标签而不删除现有的标签。当我重新加载 Recipe 对象时,tag_list 再次为空。
    • 分配标签后是否保存了Recipe对象?
    • 那我想我没有更好的主意了。对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2013-12-09
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多