这是我的方法:
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
正在向文章添加新标签,但它没有删除旧标签。
我面临的问题是更新标签将启动所有相关模型的验证,就我而言,由于我的开发计算机上没有图像和附件验证失败,验证失败。
要忽略验证,我这样做:
article.save(:validate => false)