【问题标题】:Building Rails Nested Form Within Nested Form在嵌套表单中构建 Rails 嵌套表单
【发布时间】:2015-03-20 18:30:08
【问题描述】:

我有一个包含父级、子级和子级的 rails 表单。我正在使用nested_form gem,我的rails 版本是4.1。我遇到的问题是带有嵌套字段的嵌套表单。我不想让用户使用按钮添加嵌套资源,但我希望在视图加载时实例化嵌套对象。

父模型

class LoanApplication < ActiveRecord::Base
  # Associations
  has_many :business_contacts, :dependent => :destroy

  # Nested Object 
  accepts_nested_attributes_for :business_contacts, :allow_destroy => true
end 

父母的孩子

class BusinessContact < ActiveRecord::Base
  # Associations
  belongs_to :loan_application
  has_one :personal_guarantee

  # Nested objects
  accepts_nested_attributes_for :personal_guarantee, :allow_destroy => true
end

孩子的孩子(孙子到父母)

class PersonalGuarantee < ActiveRecord::Base
  # Associations
  belongs_to :business_contact
end

查看

<%= f.fields_for :business_contacts, :wrapper => true do |contact_form| %>
    <%= contact_form.fields_for :personal_guarantee do |guarantee| %>
        <%= guarantee.text_field :guarantor_net_worth %>
    <% end %>
<% end %>

business_Contacts 字段显示正常,但 business_contacts 的子项没有显示。我已将personal_guarantee 字段嵌套在loan_application_controller 的params 哈希中。

loan_application_controller.rb

def loan_applications_params
  params.require(:loan_application).permit(:loan_application_id, business_contacts_attributes: [:id, :business_salesforce_id, :prefix, :first_name, :exp_revenue_growth, :last_name, :business_title, :is_employee, :equity_hurdle, :contact_railsid, :guarantor_gender, :guarantor_sin_number, :guarantor_date_of_birth, :guarantor_phone_number, :guarantor_street_address, :guarantor_city, :guarantor_postal_code, :guarantor_province, :_destroy, personal_guarantee_attributes: [:id, :guarantor_net_worth]]
end

如上所述,通过上述设置,我看不到表单中的 PersonalGuarantee 字段。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 nested-forms


    【解决方案1】:

    根据 rails 指南fields_for 期望选项哈希是第三个参数,第二个是对象或 nil。请试试这个:

    <%= f.fields_for :business_contacts, nil, :wrapper => true do |contact_form| %>
    

    另外,我认为表单中的所有对象都是构建的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多