为什么我的嵌套表单没有显示?

时间:2011-06-12 00:32:52

标签: ruby-on-rails-3 nested-forms

在Rails 3.1 RC1上运行并跟随this

用户可以拥有一封或多封电子邮件。我的电子邮件字段未显示在表单中。没有错误呈现。

显示表单元素。但没有输入字段。纳达。我错过了什么吗?

控制器:

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>
....

1 个答案:

答案 0 :(得分:4)

在Rails 3.1 RC中,fields_for语法看起来像changed,根据此引用:


我可以证实这一点。看来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 %>