在Rails 3.1 RC1上运行并遵循此操作。
用户可以有一封或多封电子邮件。我的电子邮件字段未显示在表单中。不会出现任何错误。
将显示表单元素。但没有输入字段。纳达。我是不是错过了什么?
控制器:
def new
@user = current_user
#1.times do
email = @user.emails.build
#end
....
end
用户型号:
class User < ActiveRecord::Base
has_many :emails, :dependent => :destroy
accepts_nested_attributes_for :emails
....
end
电子邮件型号:
class Email < ActiveRecord::Base
belongs_to :user
end
形式:
<%= form_for(@user, :url => users_path, :html => { :method => :post }) do |f| %>
<ul>
<li class="clearfix">
<%= f.fields_for :emails do |builder| %>
<%= builder.label :email %>
<%= builder.text_field :email %>
<span>Note: Your email will not be publicly displayed</span>
<% end %>
</li>
....
看起来Rails3.1RC中的fields_for
语法已经更改,如下所示:
我可以证实这一点。fields_for的标题似乎被更改为Rails 3.1rc:
Ruby on Rails最新稳定版本(v3.0.7):
fields_for(record_or_name_or_array, *args, &block)
Ruby on Rails(v3.1rc):
fields_for(record_name, record_object = nil, options = {}, &block)
现在可以使用以下内容:
<%= f.fields_for :emails, :emails do |builder| %>
<%= builder.label :email %>
<%= builder.text_field :email %>
<span>Note: Your email will not be publicly displayed</span>
<% end %>