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 }`)
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
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).