rails 4 populate dropdown values from database

后端 未结 3 719
清酒与你
清酒与你 2021-02-06 01:26

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

3条回答
  •  既然无缘
    2021-02-06 02:03

    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
    

    `

提交回复
热议问题