0
我的問題是,當試圖將子部分添加到嵌套在部分下的我的訪問表單中時,我一直在獲取參數錯誤。如何在Phoenix Framework中創建雙嵌套表單?
Interview_kit有許多部分,部分有許多子部分。部分屬於interview_kit,子部分屬於該部分。
我想用相同的形式創建所有這些,但我沒有運氣。
菲尼克斯可以這樣做嗎?
# Error
ArgumentError at GET /interview_Kit/new could not generate inputs for
:sub_sections from App.InterviewKit. Check the field
exists and it is one of embeds_one, embeds_many, has_one, has_many,
belongs_to or many_to_many
# This error is being shown when trying to render as a string in the method
link_to_sub_sections_field() below
# fields = render_to_string(__MODULE__, "sub_section.html", f: form)
# Schemas
schema "interview_kits" do
field :name, :string
field :description, :string
has_many :sections, AppSection
timestamps()
end
schema "sections" do
field :title, :string
timestamps()
belongs_to :interview_kit, App.InterviewKit
has_many :sub_sections, App.SubSection
end
schema "sub_sections" do
field :field_name, :string
belongs_to :section, App.Section
end
defmodule App.InterviewKitView do
def link_to_sub_section_fields() do
changeset = InterviewKit.changeset(%InterviewKit{sections: [%Section{sub_sections: [%SubSection{}]}]})
form = Phoenix.HTML.FormData.to_form(changeset, [])
fields = render_to_string(__MODULE__, "sub_section.html", f: form)
link "Add Sub Section", to: "#", "data-template": fields, id: "add_sub_section"
end
end
# sub_sections.html.eex
<%= inputs_for @f, :sub_sections, [multipart: true], fn fo -> %>
<div class="row">
<div class="col-md-11">
<%= input fo, :field_name %>
</div>
<div class="col-md-1">
<a href="#" id="delete_sub_section"><i class="fa fa-trash" aria-hidden="true"></i></a></h5>
</div>
</div>
<% end %>
# what I am trying to accomplish
interview_params = %{"_csrf_token" => "PDwZA==",
"_utf8" => "✓", "files" => "",
"interview_kit" => %{"description" => "<p>dsadasdasmoimoimadsads</p>",
"name" => "First Interview", "sections" => %{"0" => %{"name" => "sadasdsad"},
"sub_sections" => %{"0" => %{"field_name" => "sadasdsad"}}}}}