1

I got a backend(Namespace admin) and want to create 4 new database entries with 1 form submit(4 new countries). What I got so far:

In my countries_controller.rb

class Admin::CountriesController < Admin::AdminController
    def new
      @countries = Array.new(4) { Country.new }
    end

end

In my new.html.erb:

<%= form_for [:admin, @countries] do |f| %>   
  <% @countries.each do |country| %>
  <div class="row">
    <div class="col-md-6"> 
      <div class="form-group col-md-6">
        <%=f.text_field :country, :name, :class => "form-control", :placeholder => "Country 1" %><br>
        <%=f.text_field :country, :iso, :class => "form-control", :placeholder => "us" %>
      </div></div></div>
  <% end %>
<% end %>

But that doesn't work and I get a undefined method model_name for Array:Class error. What is the right way to do this?

1 Answer 1

2

form_for is for a single ActiveRecord object, but you're using it with an Array of object :

<%= form_for [:admin, @countries] do |f| %>   

Either create a form per object (with each form a save button):

<% @countries.each do |country| %>
  <%= form_for [:admin, country] do |f| %>   
    ...
  <% end %>
<% end %>

Or see this question for a solution using only one form: Multiple objects in a Rails form

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

1 Comment

How do I do this if I do not have the size of the array?

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.