深度嵌套 simple_form 和简单字段
deeply nested simple_form and simple fields
我的嵌套 json 负载看起来像:
{
"id": 1,
"courses": [
{
"math": [
{
"name": "Math A",
"hours": 10,
"grade": "B"
},
{
"name": "Math B",
"hours": 20,
"grade": "A"
},
{
"name": "Math C",
"hours": 10,
"grade": "B"
}
],
"english": [
{
"number_of_students": 10,
"hours": 20,
"name": "Intro to English",
"grade": "B"
},
{
"number_of_students": 15,
"hours": 25,
"name": "English 101",
"grade": "A"
}
],
},
{}, {}, {}....
]
}
我需要遍历可用的选项来生成表单,然后构建一个嵌套的负载,如上所示。
<%= simple_form_for @courses_payload, course_path(id), method: :post do |f| %>
<% available_options.each do |option| %>
<%= f.simple_fields_for Course.new do |c| %>
<%= option.name %>
<% if option.name == "english" %>
<%= c.input : number_of_students %>
<% end %>
<%= c.input :hours %>
<%= c.input :grade %>
<% end %>
<% end %>
<% end %>
api.create(
params[:courses_payload].permit(
courses:[math: [:name, :hours, :grade], english: [:number_of_students, :hours, :name, :grade]]
)
但是,我无法将所有嵌套字段都放入有效负载中。表单只能记录 grade 字段,不能记录其他嵌套字段(name, hours, number_of_students) 。我的循环有问题吗?
如果我只有散列可以使用,我会将它用于我的表单。但该表格也适用于模型,可能需要进行一些调整。
<%
hash = {
"id": 1,
"courses": [
{
"math": [
{ "name": "Math A", "hours": 10, "grade": "B" },
{ "name": "Math B", "hours": 20, "grade": "A" },
{ "name": "Math C", "hours": 10, "grade": "B" }
],
"english": [
{ "number_of_students": 10, "hours": 20, "name": "Intro to English", "grade": "B" },
{ "number_of_students": 15, "hours": 25, "name": "English 101", "grade": "A" }
],
}
]
}
%>
目标是使我们的输入 name
属性像这样:
courses_payload[courses][0][english][0][number_of_students]
<%= simple_form_for :courses_payload, url: "/courses/#{hash[:id]}", method: :post do |f| %>
<% hash[:courses].each.with_index do |courses,i| %>
<% courses.each do |course, values| %>
<%= f.simple_fields_for :courses, index: i do |ff| %>
<%# values = [{:name=>"Math A", :hours=>10, :grade=>"B"}, {:name=>"Math B", :hours=>20, :grade=>"A"}, {:name=>"Math C", :hours=>10, :grade=>"B"}] %>
<% values.each.with_index do |value, k| %>
<%# course = 'math' %>
<%= ff.simple_fields_for course, index: k do |fff| %>
<%# value = {:name=>"Math A", :hours=>10, :grade=>"B"} %>
<% value.each do |input_name, input_value| %>
<%# input_name = "hours"; input_value = "10" %>
<%= fff.input input_name, input_html: { value: input_value } %> <%# <input value="10" class="string required" type="text" name="courses_payload[courses][0][math][0][hours]" id="courses_payload_courses_0_math_0_hours"> %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
让表单提交一个实际的数组有点棘手。输入 name
属性必须如下所示:
courses_payload[courses][][english][][number_of_students]
我们需要明确地告诉表单生成器索引值为空(在某些情况下为 nil)。
<%= simple_form_for :courses_payload, url: "/courses/#{hash[:id]}", method: :post do |f| %>
<% hash[:courses].each do |courses| %>
<% courses.each do |course, values| %>
<%= f.simple_fields_for 'courses', index: '' do |ff| %>
<% values.each do |value| %>
<% value.each do |input_name, input_value| %>
<%= ff.simple_fields_for course, index: '' do |fff| %>
<%= fff.input input_name, input_html: { value: input_value } %> <%# <input value="10" class="string required" type="text" name="courses_payload[courses][][math][][hours]" id="courses_payload_courses__math__hours"> %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
我的其他几个涉及表单数组的答案:
我的嵌套 json 负载看起来像:
{
"id": 1,
"courses": [
{
"math": [
{
"name": "Math A",
"hours": 10,
"grade": "B"
},
{
"name": "Math B",
"hours": 20,
"grade": "A"
},
{
"name": "Math C",
"hours": 10,
"grade": "B"
}
],
"english": [
{
"number_of_students": 10,
"hours": 20,
"name": "Intro to English",
"grade": "B"
},
{
"number_of_students": 15,
"hours": 25,
"name": "English 101",
"grade": "A"
}
],
},
{}, {}, {}....
]
}
我需要遍历可用的选项来生成表单,然后构建一个嵌套的负载,如上所示。
<%= simple_form_for @courses_payload, course_path(id), method: :post do |f| %>
<% available_options.each do |option| %>
<%= f.simple_fields_for Course.new do |c| %>
<%= option.name %>
<% if option.name == "english" %>
<%= c.input : number_of_students %>
<% end %>
<%= c.input :hours %>
<%= c.input :grade %>
<% end %>
<% end %>
<% end %>
api.create(
params[:courses_payload].permit(
courses:[math: [:name, :hours, :grade], english: [:number_of_students, :hours, :name, :grade]]
)
但是,我无法将所有嵌套字段都放入有效负载中。表单只能记录 grade 字段,不能记录其他嵌套字段(name, hours, number_of_students) 。我的循环有问题吗?
如果我只有散列可以使用,我会将它用于我的表单。但该表格也适用于模型,可能需要进行一些调整。
<%
hash = {
"id": 1,
"courses": [
{
"math": [
{ "name": "Math A", "hours": 10, "grade": "B" },
{ "name": "Math B", "hours": 20, "grade": "A" },
{ "name": "Math C", "hours": 10, "grade": "B" }
],
"english": [
{ "number_of_students": 10, "hours": 20, "name": "Intro to English", "grade": "B" },
{ "number_of_students": 15, "hours": 25, "name": "English 101", "grade": "A" }
],
}
]
}
%>
目标是使我们的输入 name
属性像这样:
courses_payload[courses][0][english][0][number_of_students]
<%= simple_form_for :courses_payload, url: "/courses/#{hash[:id]}", method: :post do |f| %>
<% hash[:courses].each.with_index do |courses,i| %>
<% courses.each do |course, values| %>
<%= f.simple_fields_for :courses, index: i do |ff| %>
<%# values = [{:name=>"Math A", :hours=>10, :grade=>"B"}, {:name=>"Math B", :hours=>20, :grade=>"A"}, {:name=>"Math C", :hours=>10, :grade=>"B"}] %>
<% values.each.with_index do |value, k| %>
<%# course = 'math' %>
<%= ff.simple_fields_for course, index: k do |fff| %>
<%# value = {:name=>"Math A", :hours=>10, :grade=>"B"} %>
<% value.each do |input_name, input_value| %>
<%# input_name = "hours"; input_value = "10" %>
<%= fff.input input_name, input_html: { value: input_value } %> <%# <input value="10" class="string required" type="text" name="courses_payload[courses][0][math][0][hours]" id="courses_payload_courses_0_math_0_hours"> %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
让表单提交一个实际的数组有点棘手。输入 name
属性必须如下所示:
courses_payload[courses][][english][][number_of_students]
我们需要明确地告诉表单生成器索引值为空(在某些情况下为 nil)。
<%= simple_form_for :courses_payload, url: "/courses/#{hash[:id]}", method: :post do |f| %>
<% hash[:courses].each do |courses| %>
<% courses.each do |course, values| %>
<%= f.simple_fields_for 'courses', index: '' do |ff| %>
<% values.each do |value| %>
<% value.each do |input_name, input_value| %>
<%= ff.simple_fields_for course, index: '' do |fff| %>
<%= fff.input input_name, input_html: { value: input_value } %> <%# <input value="10" class="string required" type="text" name="courses_payload[courses][][math][][hours]" id="courses_payload_courses__math__hours"> %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
我的其他几个涉及表单数组的答案: