我正在为 Rails 语法苦苦挣扎。我正在尝试为用户与地址建立一对多的关系,但用户将始终拥有一个主地址。我已经设法让模型工作,但我似乎无法让表单工作。下面的表格用于“新注册”,但嵌套地址的字段不显示。
我一直在破解这个,似乎无法得到它。有人可以解释为什么在尝试注册新用户时嵌套表单字段不会显示吗?
这是表格
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => "form-horizontal"}) do |f| %>
<small>Mandatory fields marked *</small><br><br>
<%= f.fields_for :primary_address do |address_form| %>
<div class="control-group">
<%= address_form.label :first_name, :class => "control-label" %>
<div class="controls">
<%= address_form.text_field :first_name, :class => "input-xlarge" %>
</div>
</div>
<div class="control-group">
<%= address_form.label :last_name, :class => "control-label" %>
<div class="controls">
<%= address_form.text_field :last_name, :class => "input-xlarge" %>
</div>
</div>
<% end %>
这是用户模型:
class Member::User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :addresses_attributes
validates_presence_of :email
# each user may have zero or many addresses
# their primary email is also set as their login id.
has_many :addresses
has_one :primary_address, :class_name => "Address", :conditions => "is_primary = true"
accepts_nested_attributes_for :primary_address
end