これが私の方法です:
def rename
old_tag = params[:old_tag]
new_tag = params[:new_tag]
if old_tag != new_tag
# find any articles that use the old tag
Article.tagged_with(old_tag).each do |article|
# give articles with the old tag the new tag
article.tag_list.add(new_tag)
# remove the old tag
article.tag_list.remove(old_tag)
article.save
end
end
render :json => "#{old_tag} renamed to #{new_tag}"
end
私が抱えている問題は.save
、記事に新しいタグを追加することですが、古いタグを削除していないことです.