Create Multiple Records with Fields_For - Rails

Create Multiple records with fields_for - Rails

undefined method `description' for []:Array

To describe the reason for the error, fields_for accepts record_object as a second parameter which fields_for uses it to construct the fields. Here you are passing @all_list_items which is an array and that results in the error.

Solution to the error:

You should pass @list_item instead of @all_list_items

<%= fields_for :list_items, @list_item do |list_item_form| %>

Solution to the actual problem:

As you want to create multiple list_items, you should do

def new
@task = @assignment.tasks.new
x.times do
@list_item = @task.list_items.build
end
@all_list_items = []
end

where x is how many list_items you want to create, for example if you want to create three list_items, then replace x with 3.

OR

Use cocoon gem to dynamically create nested forms.

Create multiple records with a simple form on Rails

Ok, here is how i solved it, so it may help in the future

here is the forms file:

<div class="panel-body">
<div class="row">
<table class="table table-hover">
<thead class="warning">
<th><center>Color</center></th>
<th><center>Material</center></th>
<th><center>Espesor</center></th>
<th><center>Altas</center></th>
<th ><center>Bajas</center></th>
</thead>
<%= form_tag stocks_path do %>

<% @items.each do |item| %>
<%= fields_for 'data[]', @stock do |stock| %>
<tbody>
<tr>
<td><%= item.nombre %></td>
<td><center><%= item.material %></center></td>
<td><center><%= item.espesor %></center></td>
<td><center><%= stock.text_field :altas %></center></td>
<td><center><%= stock.text_field :bajas %></center></td>
<%= stock.hidden_field :item_id, :value => item.id %>
<% end %>

<% end %>
</tr>
</tbody>
</table>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<%= submit_tag "Update", class: "btn btn-primary" %>
<% end %>
</div>
</div>

And this is how the controller is working

  @stock = Stock.create(my_params(d))
if @stock.altas == nil && @stock.bajas == nil
@stock.destroy
else
@stock.save

end
@item = Item.find(@stock.item_id)
if @stock.altas != nil
@item.quantity = @stock.item.quantity + @stock.altas
end
if @stock.bajas != nil
@item.quantity = @stock.item.quantity - @stock.bajas
end
@item.save
end

redirect_to stocks_url, notice: 'Stock was successfully created.'

end

Everything works, and its also substracting and adding to the total quantity

Rails - creating multiple records of one model in one form

To do this with form_for is possible but since this is really not a form that relates to a single model instance, you really should stick to form_tag

To do it as form_for every name will have to be an attribute.

You could do (in your model)...

attr_accessor :name1, :name2, :name3

This will let the additional names be accepted even though they're not columns in the db table.

Then in your create method...

def create
%i(name name1 name2 name3).each |this_name|
User.create(name: user_params(this_name)) if user_params(this_name).present?
end
end

def user_params
params.require(:user).permit(:name, :name1, :name2, :name3)
end

Rails create multiple rows from one form

you can modify it in your building controller in create action.

def create
@building = Building.new(building_params)
if @building.save
floors = params[:number].to_i
floors.times do
Floor.create(building: @building)
end
redirect_to building_path
else
redirect_to error
end
end

in your form add a field for number of floor without erb

<%= form_with(model: building, local: true) do |form| %>
<% if building.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(building.errors.count, "error") %> prohibited this building
from being saved:</h2>

<ul>
<% building.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>

<div class="field">
<label> How many floors does the building have</label>
<input type="number" name="number"/>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>

Rails create multiple records with multiple fields

you might want to do it this way instead:

View:

<% 1.upto(2) do |i| %>
<%= text_field_tag "fields[#{i}][user]",'', :class => "user_name" %>
<%= radio_button_tag "fields[#{i}][is_checked]", '1', false %><br>
<% end %>

so you will receive something like this:

"fields" => {
"1" => {"user" => "value of 1", "is_checked" => "for 1"},
"2" => {"user" => "value of 2", "is_checked" => "for 2"}
}

then you can do this in the Controller:

params[:fields].each do |i, values| do
# where i is the i-th set
# and values are the user inputs

u = User.create(values)
end

hope this helps! =)

Create multiple records using single form (Not nested attributes)

You can try with the below solution

In your View File

<%= form_tag your_action_path do %>
<% 4.times do |i|%>
Content : <%= text_area_tag :thought_content, "", :name => "thoughts[][content]" %>
Author : <%= text_field_tag :thought_author, "", :name => "thoughts[][author]" %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>

Controller Code:

def your_action
params[:thoughts].each do |thought_params|
Thought.create(thought_params)
end
###
#Any further code#
###
end

Hope it works for you :)

How to create multiple records from a single textfield in Rails?

So this is the (desperately in need of refactoring) answer:

@post = Post.new(post_params)
@posts_collection = @post.text.to_s.split(/\r\n\r\n/)

@posts_collection.each do |post|
@post = Post.new(post_params)
@post.save
end

Basically- split up the text field into an array, and then save each item in the collection as a new record.



Related Topics



Leave a reply



Submit