2016-10-17 68 views
1

我想用form_for与非主动记录模型。Rails的form_for不填充值的字段

问题是这些字段的value未正确初始化:即使该属性存在,它始终为空。

例如这个工程:

@customer = Customer.retrieve_from_api(id) 
@customer.billing_address.first_name # => "Marco" 

但形式不正确初始化:

<%= form_for @customer, as: :customer, url: billing_info_path, method: :put do |f| %> 
    ... 
    <%= f.fields_for :billing_address do |b| %> 
    <div class="field"> 
     <%# this is always blank (i.e. the initial value of the field is "") %> 
     <%= b.text_field :first_name %> 
    </div> 

BTW,我知道,我可以用form_tagtext_field_tag并明确初始化值,但我想避免这种情况,并保持代码干,因为我有很多领域

+0

邮政.RB文件处理这个虚拟属性 – bkunzi01

+0

@ bkunzi01客户对象是这样的:https://github.com/chargebee/chargebee-ruby/blob/master /lib/chargebee/models/customer.rb – collimarco

回答

1

您必须通过@customer.billing_address作为一个参数f.fields_for,使其工作:

<%= f.fields_for :billing_address, @customer.billing_address do |b| %>