Rails中的双嵌套表单 - HTML字段为空

时间:2013-11-28 01:40:06

标签: ruby-on-rails

在我的Rails 4应用程序中,我有三个模型:

有序挑战列表(存储在ListJoin表中的位置):

class ChallengeList < ActiveRecord::Base
  has_many :list_joins, :dependent => :destroy
  accepts_nested_attributes_for :list_joins

ChallengeList-2-Challenge连接模型,包含列表挑战配对及其在挑战列表中的位置。

class ListJoin < ActiveRecord::Base
  default_scope :order => 'position'
  belongs_to :challenge
  belongs_to :challenge_list
  acts_as_list :scope => :challenge_list
  accepts_nested_attributes_for :challenge
end

挑战模型,仅包含字符串字段description

class Challenge < ActiveRecord::Base
  has_many :list_joins
  has_many :challenge_lists, :through => :list_joins 

我尝试制作一个表单,我可以同时编辑列表的挑战和位置:

= 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'

当我测试表单时,使用正确数量的字段和所有内容呈现正常,但Challenge:description:id)的可见字段为空,并且它不会为挑战id创建隐藏的字段!怎么能这样,我能解决它吗?


实施例

为身份为9的Challenge生成的字段示例,因为您可以看到非隐藏的输入字段没有value,但隐藏的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">  

1 个答案:

答案 0 :(得分:1)

我想你可能想要一个单数challenge

= links_form.fields_for :challenge do |challenge_form|