2015-08-21 9 views
5

Ho un modello chiamato Ricetta che utilizza la gemma acts-as-taggable-on per aggiungere taggings alle ricette. Il comportamento strano è quando modifico e aggiorno la ricetta tramite il controller, i taggings della ricetta sono aggiunti invece di essere aggiornati ai taggings corretti. È stato segnalato uno Github issue simile ma non c'è risposta da parte di nessuno. La mia versione di gemma act-as-taggable-on è la 3.2.6.Comportamenti strani su act-as-taggable-on gem di non taggatura cancellatura

Ad esempio, la mia ricetta ha tre taggati: fruit, fruit e vegetables. Il tag fruit non è rilevante, quindi ho rimosso questi due tag fruit nel campo di input f.tag_list per includere solo 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"=>....} 

utenti/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 

Il recipe_params inclusi tag_list come parametro consentito.

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 

ma ha finito per i vecchi marcature non vengono rimossi, e quello nuovo è stato aggiunto alla lista: fruit, fruit, vegetables, vegetables.

Un'altra cosa strana è che quando provo a vedere la tag_list della ricetta, è una matrice vuota. Ho provato a modificare il tag_list nella console di rails, ha aggiunto un altro tag ma quando faccio r.reload il tag_list è ancora una matrice vuota. Ora il rapporto tra la ricetta e le etichette sono in questo modo:

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">] 

La query di database tag_list è strano troppo, perché la query include questo taggings.tagger_id IS NULL?

Sono ancora nuovo di questa gemma, qualche idea su come aggiornare correttamente i taggings usando i metodi gem? Spero di poter evitare di aggiornare i taggings usando il mio codice per evitare ulteriori problemi. Si noti che il mio modello Recipe è versionato da paper_trail gem, spero che non sia correlato a questo problema.

+0

potete inviare le vostre 'recipe_params'? – Pavan

+0

@Pavan ha aggiunto 'recipe_params' alla domanda, grazie – nayiaw

risposta

1

Poiché non c'è alcuna soluzione da Internet e utilizzo solo questo taggable su Recipe, per ora sto chiamando manualmente un metodo per aggiornare nuovamente i tag dopo il salvataggio del modello.

Penso ancora che ci siano soluzioni migliori a questo problema, sono aperto alle alternative come la migliore risposta

1

Non riesco a vedere nulla nei documenti act-as-taggable su cosa succede quando lo tags_list passa come parametro nel metodo update_attributes, ma nel tuo caso sembra che aggiunga i tag senza rimuovere quelli esistenti. Ho notato nella documentazione che la tag_list può essere assegnato con il = che sostituisce i tag esistenti:

È inoltre possibile aggiungere e rimuovere i tag da assegnazione diretta. Nota questo rimuoverà i tag esistenti quindi usalo con attenzione.

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

Così si può fare nel vostro metodo di aggiornamento

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

Mi sono assegnato a' tag_list' nella mia console per testarlo ma i tagg sono ancora aggiunti senza rimuovere quelli esistenti. Quando ricarico l'oggetto Ricetta, 'tag_list' è di nuovo vuoto. – nayiaw

+0

Hai salvato l'oggetto Ricetta dopo aver assegnato i tag? –

+0

sì ho salvato, senza errori. Tagging è aggiunto. – nayiaw