Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
Cant create tags on rails with Select2

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
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda