Skip to content

How can a default value be set for a form.combobox tag? #167

Answered by csalmeida
csalmeida asked this question in Q&A
Discussion options

You must be logged in to vote

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:

ActiveRecord::Relation: the display is obtained by calling #to_combobox_display on each object and the value is obtained by calling #id.

This change fixed it (params[:location].to_i):

<%= 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 params[:location] and it loads correctly:

CleanShot.2024-05-17.at.11.17.14.mp4

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by csalmeida
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants