LoginSignup
3
3

More than 5 years have passed since last update.

PhoenixでmodelのListからselectタグを生成する

Last updated at Posted at 2016-01-17

概要

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>
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3