我遇到的問題是,當我在嵌套窗體中使用fields_for時,它會生成錯誤名稱的字段。fields_for爲協會生成錯誤的名稱
簡化模型是:
class User < ActiveRecord::Base
has_one :owned_account, :class_name => "Account", :foreign_key => "user_id"
accepts_nested_attributes_for :owned_account
attr_accessible :email, ... :owned_account, :owned_account_attributes
end
class Account < ActiveRecord::Base
belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'
attr_accessible :subdomain
end
在視圖(這是由的方式設計視圖)
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<div class="inputs">
<% resource.build_owned_account %>
<%= f.fields_for resource.owned_account do |a| %>
<p>
<%= a.label :subdomain, "Account handle" %>
<%= a.text_field :subdomain %>
</p>
<% end %>
<p>
...
<% end %>
它產生的標記是:
<p>
<label for="user_account_subdomain">Account handle</label>
<input id="user_account_subdomain" name="user[account][subdomain]" size="30" type="text">
</p>
所以它正在生成用戶[帳戶] [子域]而不是用戶[owned_account] [子域]。我嘗試覆蓋該字段的名稱,但可能似乎得到這個工作。
任何幫助將不勝感激,在此先感謝。
謝謝你已經做到了!我想我之前有一些類似的東西,因爲那時我並沒有使用resource.build_owned_account,謝謝你的幫助。 – slarti42uk 2011-12-22 10:34:14