无法使用 Select2 在 rails 上创建标签
Cant create tags on rails with Select2
我用 Select2.js 在 rails 上制作了自动完成标签
如果标签存在,客户端和 rails 代码可以完美运行。但是,如果我想创建或添加新标签,它就会中断。我可以创建标签,但它仍然会出错,连同我获得新命名的 ID。但我无法更改参数
document.addEventListener('turbolinks:load', selectTags)
$(document).ready(selectTags)
function selectTags() {
$('#post_tag_ids').select2({
createTag: function (params) {
return {
id: $.trim(params.term) + '#(new)',
text: $.trim(params.term)
}
},
ajax: {
url: '/tags/search',
dataType: 'json',
delay: 200,
data: function (params) {
return {
q: params.term
}
},
processResults: function (data, params) {
data.map(v => {v.text = v.name})
return {
results: data
}
},
cache: true
},
tags: true,
minimumInputLength: 2,
maximumInputLength: 20
})
}
简单 Post(标签所有者)控制器:
def create
@post = Post.new(post_params)
set_post_defaults
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: "Post was successfully created." }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def post_params
params.require(:post).permit(:category_id, :title, :content, :tag_list, :tag, { tag_ids: [] }, :tag_ids)
end
我目前的决定
before_action :create_new_tags, only: [:create, :update]
.
.
.
def create_new_tags
key = "#(new)"
tag_ids = []
params[:post][:tag_ids].each_with_index do |tag_id, index|
next if tag_id.empty?
if tag_id.include?(key) then
tag = Tag.find_or_create_by(name: tag_id.sub(key, ""))
tag.save if !tag.id
else
tag = Tag.find(tag_id.to_i)
end
tag_ids << tag.id.to_s
end
params[:post][:tag_ids] = tag_ids
end
..... 在对 rubocop 进行小的重构之后:
def create_new_tags
params[:post][:tag_ids].each_with_index do |tag_id, index|
next unless tag_id.include?("#(new)")
tag = Tag.find_or_create_by(name: tag_id.sub("#(new)", ""))
tag.save unless tag.id
params[:post][:tag_ids][index] = tag.id.to_s
end
end
实际版本here
我用 Select2.js 在 rails 上制作了自动完成标签 如果标签存在,客户端和 rails 代码可以完美运行。但是,如果我想创建或添加新标签,它就会中断。我可以创建标签,但它仍然会出错,连同我获得新命名的 ID。但我无法更改参数
document.addEventListener('turbolinks:load', selectTags)
$(document).ready(selectTags)
function selectTags() {
$('#post_tag_ids').select2({
createTag: function (params) {
return {
id: $.trim(params.term) + '#(new)',
text: $.trim(params.term)
}
},
ajax: {
url: '/tags/search',
dataType: 'json',
delay: 200,
data: function (params) {
return {
q: params.term
}
},
processResults: function (data, params) {
data.map(v => {v.text = v.name})
return {
results: data
}
},
cache: true
},
tags: true,
minimumInputLength: 2,
maximumInputLength: 20
})
}
简单 Post(标签所有者)控制器:
def create
@post = Post.new(post_params)
set_post_defaults
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: "Post was successfully created." }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def post_params
params.require(:post).permit(:category_id, :title, :content, :tag_list, :tag, { tag_ids: [] }, :tag_ids)
end
我目前的决定
before_action :create_new_tags, only: [:create, :update]
.
.
.
def create_new_tags
key = "#(new)"
tag_ids = []
params[:post][:tag_ids].each_with_index do |tag_id, index|
next if tag_id.empty?
if tag_id.include?(key) then
tag = Tag.find_or_create_by(name: tag_id.sub(key, ""))
tag.save if !tag.id
else
tag = Tag.find(tag_id.to_i)
end
tag_ids << tag.id.to_s
end
params[:post][:tag_ids] = tag_ids
end
..... 在对 rubocop 进行小的重构之后:
def create_new_tags
params[:post][:tag_ids].each_with_index do |tag_id, index|
next unless tag_id.include?("#(new)")
tag = Tag.find_or_create_by(name: tag_id.sub("#(new)", ""))
tag.save unless tag.id
params[:post][:tag_ids][index] = tag.id.to_s
end
end
实际版本here