Ruby on rails Rails中的双嵌套表单-HTML字段为空
在我的Rails 4应用程序中,我有三种型号: 挑战的有序列表(存储在Ruby on rails Rails中的双嵌套表单-HTML字段为空,ruby-on-rails,Ruby On Rails,在我的Rails 4应用程序中,我有三种型号: 挑战的有序列表(存储在ListJoin表中的位置): 我尝试制作一个表格,在其中我可以同时编辑列表中的挑战及其位置: = form_for @challenge_list do |f| -# ...fields for list... = f.fields_for :list_joins do |links_form| .field = links_form.number_field :position =
ListJoin
表中的位置):
我尝试制作一个表格,在其中我可以同时编辑列表中的挑战及其位置:
= form_for @challenge_list do |f|
-# ...fields for list...
= f.fields_for :list_joins do |links_form|
.field
= links_form.number_field :position
= links_form.fields_for :challenges do |challenge_form|
.field
-# The following fields will be empty
= challenge_form.text_field :description
= challenge_form.text_field :id -# Added to test I didn't make a typo on 'description'
当我测试表单时,它呈现OK,字段数和所有内容都正确,但是质询的可见字段(:description
和:id
)为空,并且它不会为质询id
创建隐藏字段!这是怎么回事?我能修好它吗
例子
为id为9的质询
生成的字段示例,如您所见,非隐藏输入字段没有值
,但隐藏id字段有:
<div class="field">
<input id="challenge_list_list_joins_attributes_2_challenges_description" name="challenge_list[list_joins_attributes][2][challenges][description]" type="text">
<input id="challenge_list_list_joins_attributes_2_challenges_id" name="challenge_list[list_joins_attributes][2][challenges][id]" type="text">
<br>
</div>
<!-- This id field is for the join, but there is none for the description... -->
<input id="challenge_list_list_joins_attributes_2_id" name="challenge_list[list_joins_attributes][2][id]" type="hidden" value="9">
我想您可能需要一个单一的挑战:
= links_form.fields_for :challenge do |challenge_form|
查看生成的代码,Rails似乎假定ListJoin
有许多相关的挑战(例如,[2][challenges]
),但它只有一个,因为它属于!这是一只虫子吗?天哪,真不敢相信我竟然对它视而不见。
= form_for @challenge_list do |f|
-# ...fields for list...
= f.fields_for :list_joins do |links_form|
.field
= links_form.number_field :position
= links_form.fields_for :challenges do |challenge_form|
.field
-# The following fields will be empty
= challenge_form.text_field :description
= challenge_form.text_field :id -# Added to test I didn't make a typo on 'description'
<div class="field">
<input id="challenge_list_list_joins_attributes_2_challenges_description" name="challenge_list[list_joins_attributes][2][challenges][description]" type="text">
<input id="challenge_list_list_joins_attributes_2_challenges_id" name="challenge_list[list_joins_attributes][2][challenges][id]" type="text">
<br>
</div>
<!-- This id field is for the join, but there is none for the description... -->
<input id="challenge_list_list_joins_attributes_2_id" name="challenge_list[list_joins_attributes][2][id]" type="hidden" value="9">
= links_form.fields_for :challenge do |challenge_form|