概要
PhoenixでmodelのListからselectタグを生成する。
selectの基本的な説明は以下を参照。
http://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#select/4
Example
model
role.ex
schema "roles" do
field :uid, :string
field :name, :string
end
modelのList
roles
[
{ uid: "admin", name: "Admin" },
{ uid: "user", name: "User" },
]
select
select.eex
<%= select f, :role, Enum.map(@roles, fn(x) -> {x.name, x.uid} end), value: @role.uid %>
生成されるHTML
select.html
<select id="xxx_role" name="xxx[role]">
<option selected="selected" value="admin">Admin</option>
<option value="user">User</option>
</select>