0

in my model i have this part of code for declare hard coded types

TYPES = { administrator: 1, system: 2, station: 3, guest: 4 }

    def type
      TYPES.key(read_attribute(:type_id))
    end

    def type=(s)
      write_attribute(:type_id, TYPES[s])
    end

i need get this 4 types on the view to show them as select box how can i get them

somthing like that

Admin.TYPES.each do |type|
"<option id="<%=????%>"><%=type.text%></option>"
end
4

2 回答 2

1

如果你需要选择标签,你可以使用它。
<%= select_tag('type_options',options_for_select(TYPES)) %>

rails 中的 HTML 助手。

于 2013-09-29T18:34:20.090 回答
0

How about something like this:

Admin::TYPES.each do |key, value|
  "<option id='#{value}'>#{key}</option>"
end

It should come out looking like:

<option id='1'>administrator</option>
<option id='2'>system</option>
<option id='3'>station</option>
<option id='4'>guest</option>
于 2013-09-29T18:33:22.183 回答