-
Hi 👋 I am currently trying to use <%= form.text_field :company, value: params[:company], class: "input" %> Is it possible to do the same with the combobox like so? <%= form.combobox :location, Country.all, value: params[:location] %> I noticed in the "A prefilled combobox" section in the documentation there's a way of assigning a pre-selected value by using a method? I didn't really understand that way but what I would like is to decide what the value would be myself if possible. Usually the combobox was able to work this out because I was using the following pattern: <%= form_with model: model do |form| %> However, now I am using a form with no model attached to it, it's purpose is to render inputs to filter a collection: <%= form_with url: search_employers_path, method: :get do |form| %> I also think that the component might accept My goal is to pull the I have attached a video below where we can see that when the form is submitted the selected value is cleared. Is there a way to set the value or is there a better way of achieving this? CleanShot.2024-05-16.at.19.50.25.mp4 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Okay, I think I was missing something on my side, after reading a bit more of the source code it did seem like Then I found this in the documentation which made me think that maybe the
This change fixed it ( <%= form.combobox :location, Country.all, id: "location", label: nil, value: params[:location].to_i %> Here's a demo where the value is being picked from CleanShot.2024-05-17.at.11.17.14.mp4 |
Beta Was this translation helpful? Give feedback.
-
Great digging! I'll reopen this so I remember to consider casting the value so you don't have to do that yourself. |
Beta Was this translation helpful? Give feedback.
Okay, I think I was missing something on my side, after reading a bit more of the source code it did seem like
combobox
could take a:value
parameter.Then I found this in the documentation which made me think that maybe the
:value
needs to be an integer by default:This change fixed it (
params[:location].to_i
):Here's a demo where the value is being picked from
params[:location]
and it loads correctly:CleanShot.2024-05-17.at.11.17.14.mp4