I made a rails application. I am supposed to implement has_many, belong_to relations between Author, and Location models as follows, and wrote something as:
In models:
class Author < ActiveRecord::Base
belongs_to :location
end
class Location < ActiveRecord::Base
has_many :authors
accepts_nested_attributes_for :authors
end
In 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 %>
My question is, even the location can have many authors, it is showing only one text field for author for the fields_for :authors, then how can I achieve to append as many as text fields for authors according to the user wish?
Can anyone help me please?
Beautiful tutorial from Ryan Bates:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2