The problem I’m having is that when I’m using fields_for in a nested form, it’s generating the fields with the wrong name.
The simplified models are:
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
In the view (this is a Devise view by the way)
<%= 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 %>
The markup it generates is:
<p>
<label for="user_account_subdomain">Account handle</label>
<input id="user_account_subdomain" name="user[account][subdomain]" size="30" type="text">
</p>
So it’s generating user[account][subdomain] instead of user[owned_account][subdomain]. I’ve tried overriding the name of the field but could seem to get that to work.
Any help would be most appreciated, thanks in advance.
In order to trigger the nested attributes magicness you need to do
If you give an actual instance of account rails doesn’t know that it’s the one relating to the
owned_accountassociation