Replies: 1 comment 3 replies
-
Hey @moneill thanks for the question I believe if you use the form builder syntax this should just work without passing a The reasoning behind this is — In your case, the value and display are the same thing. But this doesn't always happen. So we can't assume the passed value can be used as a display every time (although it might be a sensible default, a discussion for another day!). By having an object to call Please try this and let me know if it works: First, update to the latest v0.2.3 (earlier versions don't support non-AR objects in the form builder). Then, create a form object that responds to class AddressSearch
include ActiveModel::Model
attr_accessor :trip
def search
trip.formatted_address
end
end
>> AddressSearch.new(trip: OpenStruct.new(formatted_address: "Chicago, IL, USA")).search
=> "Chicago, IL, USA" Instantiate it in your controller: class TripsController < ApplicationController
def new
@address_search = AddressSearch.new(trip: OpenStruct.new(formatted_address: "Chicago, IL, USA")).search
end
end Finally, use the form builder syntax to build your combobox: <%= form_with model: @address_search, url: "#" do |form| %>
<%= form.combobox :search, autocomplete_path(category: "region"), spellcheck: false, placeholder: "Begin typing a location name", data: { action: "hw-combobox:selection->autocomplete#startSelected", autocomplete_target: "startSearch" } %>
<% end %> This is admittedly cumbersome. Maybe we should allow passing an object to One last thing is that this dance isn't necessary in synchronous comboboxes because all the options are loaded, so we can look for the option in-memory and get its display value from there. |
Beta Was this translation helpful? Give feedback.
-
I have an async combobox that queries an address autocomplete API. I've read through the docs and scanned through #123 as well, but I can't seem to accomplish what I'm after.
Here's my combobox code:
In the DOM, I can see that the hidden input's value is correctly set to the value of
trip.formatted_address
, but the combobox itself still renders theplaceholder
string. I would ideally like the combobox to be prefilled with the value oftrip.formatted_address
, which isChicago, IL, USA
in this example. Anyone have pointers on how I might accomplish that? I can sorta hack it for now by doingplaceholder: trip.formatted_address || "Begin typing a location name"
but that's not ideal.Beta Was this translation helpful? Give feedback.
All reactions