2016-10-17 69 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| %>