如何让标签在更新时持久化?
How to Make Tags Persist When Updating?
标签在以下期间持续存在:
def update
if @goal.update(goal_params)
respond_modal_with @goal, location: root_path
else
render action: 'edit'
end
end
但是当我通过以下方式更新目标时:
def mark_accomplished
@goal.update(accomplished: true)
respond_to do |format|
format.html
format.js { render :nothing => true }
end
end
当用户点击 mark_accomplished
按钮时,该目标的标签就会消失:
<%= link_to 'Accomplished', mark_accomplished_path(goal), remote: true, method: 'put', %>
路线:
get '/goal_signup/', to: 'goals#goal_signup', as: 'goal_signup'
和标签(gem 'acts-as-taggable-on'
)有关系,因为我习惯MVC出现类似情况。我在这里使用目标作为主要示例,但如果您也想要习惯来提供更多背景信息,请告诉我。
goal.rb
class Goal < ActiveRecord::Base
scope :publish, ->{ where(:conceal => false) }
belongs_to :user
acts_as_taggable
scope :accomplished, -> { where(accomplished: true) }
scope :unaccomplished, -> { where(accomplished: nil) }
before_save :set_tag_owner
def set_tag_owner
# Set the owner of some tags based on the current tag_list
set_owner_tag_list_on(self.user, :tags, self.tag_list)
# Clear the list so we don't get duplicate taggings
self.tag_list = nil
end
end
标签控制器
class TagsController < ApplicationController
def index
@tags = ActsAsTaggableOn::Tag.most_used(20)
# The tags for all users and habits.
@special_tags = ActsAsTaggableOn::Tag.includes(:taggings)
.where('taggings.taggable_type' => ['Habit','User','Valuation','Goal'])
.most_used(10)
# to get tags where the current user is the tagger
@user_tags = current_user.owned_tags
end
def show
@tag = ActsAsTaggableOn::Tag.find(params[:id])
end
end
db
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true, using: :btree
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
create_table "tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree
邪恶是before_save :set_tag_owner
,你设置了self.tag_list = nil
。
因此,在保存目标之前,您的标签列表会被清空。
它正在更新操作中工作,因为您正在再次设置标签。
要解决此问题,请仅在要删除标签时将 nil 分配给 tag_list。
标签在以下期间持续存在:
def update
if @goal.update(goal_params)
respond_modal_with @goal, location: root_path
else
render action: 'edit'
end
end
但是当我通过以下方式更新目标时:
def mark_accomplished
@goal.update(accomplished: true)
respond_to do |format|
format.html
format.js { render :nothing => true }
end
end
当用户点击 mark_accomplished
按钮时,该目标的标签就会消失:
<%= link_to 'Accomplished', mark_accomplished_path(goal), remote: true, method: 'put', %>
路线:
get '/goal_signup/', to: 'goals#goal_signup', as: 'goal_signup'
和标签(gem 'acts-as-taggable-on'
)有关系,因为我习惯MVC出现类似情况。我在这里使用目标作为主要示例,但如果您也想要习惯来提供更多背景信息,请告诉我。
goal.rb
class Goal < ActiveRecord::Base
scope :publish, ->{ where(:conceal => false) }
belongs_to :user
acts_as_taggable
scope :accomplished, -> { where(accomplished: true) }
scope :unaccomplished, -> { where(accomplished: nil) }
before_save :set_tag_owner
def set_tag_owner
# Set the owner of some tags based on the current tag_list
set_owner_tag_list_on(self.user, :tags, self.tag_list)
# Clear the list so we don't get duplicate taggings
self.tag_list = nil
end
end
标签控制器
class TagsController < ApplicationController
def index
@tags = ActsAsTaggableOn::Tag.most_used(20)
# The tags for all users and habits.
@special_tags = ActsAsTaggableOn::Tag.includes(:taggings)
.where('taggings.taggable_type' => ['Habit','User','Valuation','Goal'])
.most_used(10)
# to get tags where the current user is the tagger
@user_tags = current_user.owned_tags
end
def show
@tag = ActsAsTaggableOn::Tag.find(params[:id])
end
end
db
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true, using: :btree
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
create_table "tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree
邪恶是before_save :set_tag_owner
,你设置了self.tag_list = nil
。
因此,在保存目标之前,您的标签列表会被清空。
它正在更新操作中工作,因为您正在再次设置标签。
要解决此问题,请仅在要删除标签时将 nil 分配给 tag_list。