插入具有has_many关联的模型

时间:2015-07-16 12:49:21

标签: elixir phoenix-framework

我有以下型号:

# Foo model
schema "foo" do
  field :name, :string
  has_many: :bars, App.Bar
end

# App model
schema "bar" do
  field :name, :string
  belongs_to: foo, App.Foo
end

这种形式:

# form.html (Foo)
<%= form_for @changeset, @action, fn f -> %>
  <%= text_input f, :name, class: "form-control" %>
  <%= submit "Submit", class: "btn btn-primary" %>
<% end %>

在此表单中,如何添加文本字段以使用Foo填充新的Bars

以下操作不起作用,因为bars未预加载:

<%= text_input f, :bars, class: "form-control" %>

我是否在正确的轨道上?如果是这样,我如何在表单中预加载Bars

更新,控制器:

def new(conn, _params) do
  changeset = %Foo{} |> Repo.preload(:bars) |> Foo.changeset
  render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"foo" => foo_params}) do
  changeset = %Foo{} |> Repo.preload(:bars) |> Foo.changeset(foo_params)

  if changeset.valid? do
    Repo.insert!(changeset)

    conn
    |> put_flash(:info, "Foo created successfully.")
    |> redirect(to: foo_path(conn, :index))
  else
    render(conn, "new.html", changeset: changeset)
  end
end

预加载似乎有效,但到达时我得到Argument error <%= text_input f, :bars, class: "form-control" %>

[error] #PID<0.280.0> running App.Endpoint terminated
Server: 192.168.48.202:4000 (http)
Request: GET /
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.bit_size([])
        (phoenix_html) lib/phoenix_html/tag.ex:66: anonymous fn/2 in Phoenix.HTML.Tag.tag_attrs/1
        (elixir) lib/enum.ex:1261: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_html) lib/phoenix_html/tag.ex:35: Phoenix.HTML.Tag.tag/2
        (app) web/templates/foo/form.html.eex:16: anonymous fn/1 in App.FooView.form.html/1
        (phoenix_html) lib/phoenix_html/form.ex:181: Phoenix.HTML.Form.form_for/4
        (app) web/templates/foo/form.html.eex:1: App.FooView."form.html"/1
        (app) web/templates/foo/new.html.eex:3: App.FooView."new.html"/1

1 个答案:

答案 0 :(得分:13)

查看Jose在Working with Associations and Embeds上的帖子,了解使用ToDoLists和ToDoItems的强大示例(特别是标题为&#34;嵌套关联和嵌入&#34; 的部分)。下面的示例是一个衍生示例,反映了 Foos 条形的组合。

首先,您要通过以下方式走上正轨:
has_many: :bars, App.Bar

修改表单以反映:

# form.html (Foo)
<%= form_for @changeset, @action, fn f -> %>
  <%= text_input f, :name, class: "form-control" %>
  <%= inputs_for f, :bars, fn i -> %>
    <div class="form-group">
      <%= label i, :name, "Bar ##{i.index + 1}", class: "control-label" %>
      <%= text_input i, :name, class: "form-control" %>
  </div>
<% end %>
  <%= submit "Submit", class: "btn btn-primary" %>
<% end %>

这利用inputs_for/4中的Phoenix.HTML.Form函数为您的:bars关联生成字段。在这里,我们按顺序标记每一个,&#34; Bar#1&#34; &#34; Bar#2&#34;以及为每个提供text_input标记。

现在,您必须调整控制器的newcreate操作以反映包含一些条形图(例如,让两个条形图):

def new(conn, _params) do
  changeset = %Foo{} |> Foo.changeset(%Foo{bars: [%MyApp.Bar{}, %MyApp.Bar{}]})
  render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"foo" => foo_params}) do
  changeset = %Foo{} |> Foo.changeset(foo_params)

  case Repo.insert(changeset) do

    conn
    |> put_flash(:info, "Foo created successfully.")
    |> redirect(to: foo_path(conn, :index))
  else
    render(conn, "new.html", changeset: changeset)
  end
end

您的editupdate操作需要预加载条:

foo = Repo.get!(Foo, id) |> Repo.preload(:bars)