2

I want to create multiple instances of the same object in one form. I have tried

<%= form_tag(animals_path) do %>
 <% @animals.each do |animal| %>
  <%= fields_for "animals[]",animal do |f| %>
   `//animal fields
 <% end %>
<%= submit_tag 'save' %>`

but got error: "undefined method `merge!' for nil:NilClass"

I have built @animals at my controller like this

@animals = []
 3.times do |t|
 @animals << Animal.new
end

using rails 3.0.7

any help is appreciated.

5
  • can you post your controller code. Commented Oct 21, 2015 at 9:42
  • I have edited my post on that Commented Oct 21, 2015 at 9:59
  • You want to use nested attributes. For nested attributes there must be a parent model. But in your case you use form_tag . so when it tries to merge child attributes to parent model, it didn't find any parent model so the error is produced. Commented Oct 21, 2015 at 10:09
  • for more details check this api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/… Commented Oct 21, 2015 at 10:11
  • I can't do it in nested attributes, because i have no parent model. do you have any idea how to do it without nested attributes? Commented Oct 21, 2015 at 10:15

1 Answer 1

1

I think this is not possible with nested form you can use your own trick to do this. There is some questions on stack overflow like this How to submit multiple, duplicate forms from same page in Rails - preferably with one button

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.