Jump to Particular Index in for loop Phoenix HTML?

Get rid of the for comprehension and define a function in the view to do the work

<%= form_for second_changeset(@candidate_pipelines), "#", [class: "d-inline", phx_change: "update_candidate_stage"], fn f -> %>
  <input type="hidden" value="<%= candidate_pipeline.id %>" name="candidate_id"/>
  <%= select f, :job_stage_id, Enum.map(@job_stages, &{&1.name, &1.id}), [class: "custom-select custom-select-sm"] %>
<% end %>

For example you can pick the second element in a list this way:

iex(1)> second = fn [_, value| _] -> value end
#Function<6.128620087/1 in :erl_eval.expr/5>
iex(2)> list = [1,2,3]
[1, 2, 3]
iex(3)> second.(list)
2
iex(4)>

Example of defining view functions (page_view.ex) being used in index.html.eex

1 Like