Reputation:
I want to do this: I have a Model called Item with a field called name generated with scaffold:
rails g scaffold Item name:string
My question is: When I have to create a new Item I go in localhost/3000/new and I only can create a Item each time. How I can repeat automaticaly n times? Each time i push create I want to create n registers. Does Rails Have an easy way to do this. Thanks and sorry for my English level.
Upvotes: 0
Views: 474
Reputation: 1311
You can do it by using loop for n times in your form. Like:
<% n= 10 %>
<%= form_tag url: "/products",method: :post do|f| %>
<% (0..n).times do |i| %>
<label> Name </label>
<%= text_field_tag "products[][:name]"%>
<% end %>
<%= submit_tag "Create Products" %>
<% end %>
And, in products controller, Write following code
def create
@products = Product.create(params[:products])
end
Upvotes: 0
Reputation: 51
Try to use Faker gem and simple rake task /lib/tasks/task.rake with following code:
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
make_items
end
end
def make_items
99.times do |n|
name = Faker::Name.name
Item.create!(name: name)
end
end
Upvotes: 1