I have a model called Recipe that uses the acts-as-taggable-on gem to add tags to recipes. The strange behavior is when I edit and update the recipe through the controller, the recipe tags are added instead of updating them to the corresponding tags. A similar Github issue has been reported, but no answer from anyone. My version with the effect of "as-taggable-on" 3.2.6.
For example, my recipe has three labels: fruit , fruit and vegetables . The fruit tag does not matter, so I removed these two fruit tags in the f.tag_list input field to include only 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"=>....}
users / 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 included tag_list as an allowed parameter.
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
But in the end, the old tags are not deleted, and the new one is added to the list: fruit , fruit , vegetables , vegetables .
Another oddity is that when I try to see tag_list from a recipe, it is an empty array. After trying to edit tag_list in the rails console, he added another tag, but when I do r.reload tag_list , it's still an empty array. Now the link between the recipe and the tags looks like this:
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">]
The tag_list database tag_list also strange, so the query includes this taggings.tagger_id IS NULL ?
I'm still new to this stone, any idea on how to update tags correctly using gem methods? Hopefully I could avoid updating the tags using my own code to avoid further problems. It is noted that my recipe model has a paper_trail version, I hope this is not related to this problem.