【问题标题】:multiple nested forms within a ruby on rails projectruby on rails 项目中的多个嵌套表单
【发布时间】:2016-06-12 10:26:50
【问题描述】:

我试图在一个表单中使用多个嵌套属性,但似乎只出现了第一组属性。

以下是 HAML 模板

.mdl-cell.mdl-cell--10-col.mdl-cell--1-offset
  %h3.mdl-typography--display-1= t('.account_title')
  = form.fields_for :address do |address_fields|
    .mdl-grid
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
        = address_fields.text_field :first_name, class: 'mdl-textfield__input'
        = address_fields.label :first_name, class: 'mdl-textfield__label'
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
        = address_fields.text_field :last_name, class: 'mdl-textfield__input'
        = address_fields.label :last_name, class: 'mdl-textfield__label'
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
        = form.text_field :name, class: 'mdl-textfield__input'
        = form.label :name, class: 'mdl-textfield__label'
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
        = form.email_field :email, class: 'mdl-textfield__input'
        = form.label :email, class: 'mdl-textfield__label'
  %h3.mdl-typography--display-1= t('.bank_title')
  = form.fields_for :account do |account_fields|
    .mdl-grid
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
        = account_fields.text_field :bsb, class: 'mdl-textfield__input'
        = account_fields.label :bsb, class: 'mdl-textfield__label'
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
        = account_fields.text_field :account_number, class: 'mdl-textfield__input'
        = account_fields.label :account_number, class: 'mdl-textfield__label'
      .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--12-col
        = account_fields.text_field :account_name, class: 'mdl-textfield__input'
        = account_fields.label :account_name, class: 'mdl-textfield__label'

模型中的定义

accepts_nested_attributes_for :address, reject_if: proc { |attributes| attributes['address1'].blank? }
  accepts_nested_attributes_for :trading_hours
  accepts_nested_attributes_for :phones, :reject_if => lambda { |t| ( t['display_number'].gsub(/\D+/, '').blank?) }
  accepts_nested_attributes_for :account

【问题讨论】:

  • 你能显示从你的 Rails 服务器日志传递的参数吗?

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


【解决方案1】:

在父级上你需要为表单构建每个附加属性(这是一个新表单,我相信你可以在控制器中的 def new... 下做到这一点)

def new
  @parent.address.build
  @parent.trading_hours.build
  @parent.phones.build
  @parent.account.build
end

如果您不尝试制作新表格,请告诉我并更新问题

【讨论】:

  • 这似乎是找到,但根据 Rubymine 告诉我的文档,这与 Rails 方式相悖
  • 哈哈,如果你这么说,好吧!如果对象上没有构建属性,属性将不会显示。
  • 此代码不应在控制器中,因为只应调用 1 个方法。这不是最佳实践,它违背了 FAT 模型、Skinny Controllers
  • 好吧,如果 ruby​​mine 告诉你我不知道该说什么。这是解释的链接。如果不构建他们不会呈现的属性。我想我还没有做最佳实践...@9.2 中的guides.rubyonrails.org/form_helpers.html#building-complex-forms“如果一个人没有地址,它什么都不会呈现。”
  • 我不知道你为什么把我的答案记下来,它解决了问题。在环顾四周以检查它还能去哪里之后,人们似乎把它放在了两个地方,在帮助器下的视图中或在控制器中......无论哪种方式,你都需要使用构建来让它工作。 .. -1 有点不酷。
猜你喜欢
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多