atstockland said over 2 years ago on Slim Select with Stimulus :
Hi Dave!  I'm swapping select2 for slim-select.  I followed this video, but have run into a snag I can't seem to solve.

the view

  <%= f.select :location_id,
    [@existing_location&.name],
    { },
    class: "form-control select",
    data: { controller: 'slimselect', 'slimselect-url-value': ajax_locations_url } %>

locations_controller

  def ajax_locations
    if params[:q].present?
      @locations = Location.where('municipality iLIKE :q OR state iLIKE :q OR local_code iLIKE :q OR name iLIKE :q OR country iLIKE :q', q: "%#{ params[:q] }%")
      render json: @locations.map { |loc| { value: loc.id, text: loc.name } }
    else
      render json: []
    end
    skip_authorization
  end

slimselect_controller.js

  connect() {
    let url_val = this.urlValue
    new SlimSelect({
      select: this.element,
        searchingText: "Searching...",
        ajax: function (search, callback) {
          if (search.length < 3) {
            callback('need 3 characters to search')
            return
          }

          let url = new URL(url_val)
          url.search = new URLSearchParams({ q: search })
          console.log(`url: ${ url }`)

          fetch(url)
            .then(response => response.json())
            .then(json => callback(json))
            .catch(error => callback(error))
        }
    })
  }


I'm not getting any console errors, and I can manually render the url in a separate window and see the result.  Example: http://localhost:3000/ajax_locations.json?q=reno
[{"value":22500,"text":"Reno, NV  (US/Pacific)"},{"value":22080,"text":"KRTS - Reno, NV  (US/Pacific) [KRTS]"}]
However, nothing happens in the view...no options are added to the select input.  Can anyone see where I may have gone wrong?

EDIT:  the problem was the initial value being set in the select.  By setting the select f.select [], {} it now works.  So, now I need to figure out how to set/select an initial value (like on edit).

Thanks so much!