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?

Evo_x
  • 2,997
  • 5
  • 24
  • 40

1 Answers1

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

Community
  • 1
  • 1
Baldrick
  • 23,882
  • 6
  • 74
  • 79