I make autocomplete tags on rails with Select2.js Client and rails code perfectly works, if tags exsist. But if I want to create or add new tag it breaks. I can create tags, but it still makes error, along ids I get new named. but I can't change params
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
})
}
Simple Post (tags owner) controller:
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
My current decision
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
.... and after small refactoring for 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
Actual version here