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