nay*_*iaw 5 ruby ruby-on-rails acts-as-taggable-on ruby-on-rails-4
我有一个名为Recipe的模型,它使用acts-as-taggable-on gem来为食谱添加标签.奇怪的行为是当我通过控制器编辑和更新配方时,添加配方标签而不是更新到正确的标签.有一个类似的Github问题报道,但没有人回复.我的act-as-taggable-on gem版本是3.2.6.
例如,我的食谱有三个引用的Tagging: fruit,fruit和vegetables.该fruit标签是不相关的,所以我删除这两个fruit在我的标签f.tag_list输入字段只包括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"=>....}
Run Code Online (Sandbox Code Playgroud)
用户/ 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
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
但结束了旧的Tagging不会被删除,而新的一个已经被添加到列表中:fruit,fruit,vegetables,vegetables.
另一个奇怪的事情是,当我尝试查看配方的tag_list时,它是一个空数组.试图编辑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">]
Run Code Online (Sandbox Code Playgroud)
该tag_list数据库查询是太奇怪了,为什么查询包括本taggings.tagger_id IS NULL?
我还是这个宝石的新手,关于如何使用gem方法正确更新标签的任何想法?我希望我可以避免使用自己的代码更新标记以避免进一步的问题.值得注意的是我的Recipe模型是由paper_trail gem 版本化的,我希望它与这个问题无关.
由于互联网上没有解决方案,而且我只在 Recipe 上使用此可标记,所以现在我手动调用一个方法来在模型保存后重新更新标签。
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
Run Code Online (Sandbox Code Playgroud)
我仍然认为这个问题有更好的解决方案,我愿意接受替代方案作为最佳答案。
| 归档时间: |
|
| 查看次数: |
224 次 |
| 最近记录: |