[英]Rails form_for password_field helper doesn't populate current password
[英]Rails form_for doesn't populate fields with values
我想將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>
順便說一句,我知道我可以使用form_tag
、 text_field_tag
並顯式初始化值,但我想避免這種情況並保持代碼干燥,因為我有很多字段
您必須將@customer.billing_address
作為參數傳遞給f.fields_for
才能使其工作:
<%= f.fields_for :billing_address, @customer.billing_address do |b| %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.