Rails - Creating a select tag from a object hash

前端 未结 4 2109
无人及你
无人及你 2021-02-18 23:13

I need to create a select box from the values available in a Hash.

For instance, I have a \'thing\' and the \'thing\' has a variety of status fields:

1 =         


        
4条回答
  •  灰色年华
    2021-02-18 23:58

    Hash.each |a| returns an array of the form a = [key, value], so for the hash @status_fields you can write:

    <%= collection_select('thing', 'status', @status_fields, :first, :last) %>
    

    Alternatively, if you'd like the key to show up in the select list and the value point to the select list value, then:

    <%= collection_select('thing', 'status', @status_fields, :last, :first) %>
    

提交回复
热议问题