How to generate a form that edit and creates all the records of a model in Ruby on Rails?

59 views Asked by At

I have a model that stores template data and is independent with other models. I want to construct a form where I can view all the records of that model, edit them, delete them and also create a new records. Since rails form helper requires a record, is there any way to achieve my requirement or do I have to write the form manually and handle the deleted records and new records with js ?

1

There are 1 answers

1
Daniel Westendorf On

You'll want to iterate over all our records and then render a form for each record in itself. Here is how you might do that:

# resource/index.html.erb
// all existing records
<%= render partial: "form", collection: @all_resource_records, as: :resource %>

// new record
<%= render partial: "form", locals: { resource: ResourceModel.new } %>

-

# resource/_form.erb
<%= form_for resource do |f| %>
  // ... form fields ...
  <%= f.submit %>
<% end %>