24sharon
24sharon

Reputation: 1975

Rails get list of types (enum)

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

Upvotes: 0

Views: 1202

Answers (2)

skadoosh
skadoosh

Reputation: 481

If you need select tag, you can use this.
<%= select_tag('type_options',options_for_select(TYPES)) %>

HTML Helpers in rails.

Upvotes: 1

trh
trh

Reputation: 7339

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>

Upvotes: 0

Related Questions