How to generate many of the same form fields
I have a Product
Model with the below table which has a ProductsController
and Form:
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.decimal :price
t.string :location
end
How can i generate name
and price
again six times on my form to make six products ( six is a number i just threw out there) in the database?
EDIT: Added the :location
to give more of an explanation of what I'm trying to do.
Only on one form Users can create products and can have as many products as they choose ( :name
& :price
) to be made and all have the same :location
when 开发者_StackOverflowthey submit the form. This is basically a way to do Nested Models but with one table only ( including the Railscast Ajax, i will put this in myself).
Rails has support for input arrays. Things like products[] can be submitted to the controller
Otherwise, you'd run into issues submitting a form with inputs with the same name and ID. You could bundle all the inputs up into a Javascript array and submit that via POST.
If you're using jQuery, you could name all your product name inputs like this:
<input class="product_name" type="text">
<input class="product_name" type="text">
<input class="product_name" type="text">
do something like
var products_array = [];
$(".products").each(function() {
products_array.push($(this).val());
});
Out of curiosity, why do you want to enter multiple product on one page? Is this like an invoice type thing?
精彩评论