I'm trying to populate a field :params (from a Model/Schema) which is a map. I've got a working form_for and I'd like to populate this :params map through checkboxes so that when the form is submitted the controller will receive something like %{... params => %{"param1" => "true", "param2" => "false"}} ...
I've looked at inputs_for, but that doesn't seem to do what I need since it relies on nested schemas and models, and that would mean I need to create a new schema for each new set of parameters (I need something generic that doesn't need changes to the source code if the parameters change).
<%= form_for @changeset, audit_audit_path(@conn, :new_tool_request, @audit.id),
fn f -> %>
<%= render LayoutView, "changeset_error.html", conn: @conn, changeset: @changeset, f: f %>
<div class="form-group"><label>Tool</label>
<%= select f, :toolname, tools %>
</div>
<div class="form-group"><label>Parameter 1</label>
<%= checkbox f, :param1 %>
</div>
<div class="form-group"><label>Parameter 2</label>
<%= checkbox f, :param2 %>
</div>
<div class="form-group"><label>Date of Execution</label>
<%= datetime_select f, :date %>
</div>
<div class="form-group">
<%= hidden_input f, :audit_id, value: @audit.id %>
</div>
<%= submit "Request", class: "btn btn-primary" %>
<% end %>
So instead of having those checkboxes for param1
and param2
, I need to get all those parameters into a map. If another form is rendered with different checkboxes for the parameters, it must populate without having any relationship to a schema.
Thanks!
<%= checkbox f, :param1, name: "params[param1]" %>
and<%= checkbox f, :param2, name: "params[param2]" %>
but I believe with form_for it should be done automatically. – Roomerdef action_to_use(conn, "form" => form, "params" => params
and you will get the second form you created with the namespace. – Pee