ruby - Rails nested form creation -
i have app in want create nested form
my models:
class abonent < activerecord::base belongs_to :town has_many :numbers, :dependent => :destroy accepts_nested_attributes_for :numbers end class number < activerecord::base belongs_to :abonent end
abonents controller:
class abonentscontroller < applicationcontroller def new @abonent = abonent.new end def create @abonent = abonent.new abonents_params if @abonent.save redirect_to :towns else render action: "new" end end private def abonents_params params.require(:abonents).permit( :fullname, :work_position, :department, :email, :town_id, numbers_attributes: [ :phone ] ) end end
and abonents view
<hr><br> <%= form_for :abonents, url: abonents_path |f| %> <%= f.label :fullname %>: <%= f.text_field :fullname %><br /> <br /> <%= f.label :work_position %>: <%= f.text_field :work_position %><br /><br /> <%= f.label :department %>: <%= f.text_field :department %><br /><br /> <%= f.label :email %>: <%= f.text_field :email %><br /><br /> <%= f.label :town_id %>: <%= f.select :town_id, town.all.collect { |p| [ p.ru_name, p.id ] } %><br /><br /> <%= f.fields_for :numbers |phones|%> <%= phones.label :phone %>: <%= phones.text_field :phone %><br /><br /> <% end %> <%= f.submit %> <% end %>
the problem when submit form, creates abonent, doesn't create number abonent.
i saw many different manuals , couldn't find error in code. please help. thank you.
upd added repo on github problem.
you need build
associated records
def new @abonent = abonent.new @abonent.numbers.build end
Comments
Post a Comment