如何在has_many,belongs_to关系中向'fields_for'添加多个文本字段?

时间:2013-01-03 05:35:44

标签: ruby-on-rails

我做了一个rails应用程序。我应该按照以下方式在has_manybelong_to模型之间实现AuthorLocation关系,并写下如下内容:

在模特中:

class Author < ActiveRecord::Base
  belongs_to :location
end

class Location < ActiveRecord::Base
  has_many :authors
  accepts_nested_attributes_for :authors
end

在location / new.html.erb中:

<%= form_for(@location) do |f| %>
  <%= f.label :location_name %>
  <%= f.text_field :location_name %>
  <%= f.fields_for :authors do |a| %>      
    <%= a.label :name %>
    <%= a.text_field :name %>
  <% end %>
<% end %>

我的问题是,即使该位置可以有很多作者,它只为fields_for :authors的作者显示一个文本字段,然后我如何实现为作者添加尽可能多的文本字段用户希望?

有人可以帮我吗?