I have a dropdown in a rails form:
<%= f.select :lists, [["test1", 1], ["test2", 0]] %>
This works fine but how can I make it dynamic. (interacting with model data)
I have a controller with an action containing @list = List.all
How can I populate id
and name
in my combobox
. I've been searching around, but I am unclear about it. Can anyone help>
This worked for me
# view
<%= form.select(:list_id) do %>
<% @list.each do |l| -%>
<%= content_tag(:option, l.name, value: l.id) %>
<% end %>
<% end %>
and
# controller
@list ||= List.all
`
You can use options_from_collection_for_select.
<% options = options_from_collection_for_select(@list, 'id', 'name') %>
<%= f.select :all_val, options %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With