关联字段未从数据库加载值

时间:2016-08-15 09:05:13

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

我正在尝试获取嵌套属性表单以检测已加载到数据库中的值,任何人都可以看到此代码有什么问题

控制器

def edit
    @merchant = Merchant.find(params[:id])
    @states = State.form_selector
    @merchant.build_address if @merchant.address1.nil?
    @merchant.build_account if @merchant.bsb.nil?
    @products = Product.all
  end

视图

= form.fields_for :address do |address_fields|
    = address_fields.hidden_field :address_type_id, value: 4
    = address_fields.hidden_field :default, value: true
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--12-col
      = address_fields.text_field :address1, class: 'mdl-textfield__input'
      = address_fields.label :address1, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--12-col
      = address_fields.text_field :address2, class: 'mdl-textfield__input'
      = address_fields.label :address2, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--6-col
      = address_fields.text_field :city, class: 'mdl-textfield__input'
      = address_fields.label :city, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--2-col
      = address_fields.text_field :zip_code, class: 'mdl-textfield__input', :pattern => '^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$'
      = address_fields.label :zip_code, class: 'mdl-textfield__label'
      %span.mdl-textfield__error= t('.postcode_error')
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label.mdl-cell.mdl-cell--3-col
      = address_fields.select   :state_id, @states, :include_blank => true
      = address_fields.label :state_id, class: 'mdl-textfield__label'

0 个答案:

没有答案