0

if i make one scaffold code like this

rails g scaffold Order name:string item:string
rake db:migrate

then in 'app/views/orders/_form.html.erb' there is form for one record of Order model

and in orders_controller.rb

def new
  @order = Order.new
end

but i want to get multiple record for Order in one form

how can i do this? I tried

def new
  @orders = []
  4.times do
     @orders << Order.new
  end
end

but after this code i cannot do anything...can anyone help me please

PrepareFor
  • 2,448
  • 6
  • 22
  • 36
  • Well, you are on a good track, but a lot more needs to be done (and out of scope for a SO question). I recommend going through [this](http://vicfriedman.github.io/blog/2015/07/18/create-multiple-objects-from-single-form-in-rails/) tutorial and then come back with more specific questions. – Gerry May 14 '17 at 12:09
  • yes i already read that post, but that writer just use Model, and i want use Scaffold. I'm confusing that form_for in scaffold and form_tag for model – PrepareFor May 14 '17 at 13:39
  • The reason behind changing `form_for` to `form_tag` is to have flexibility for setting order params as an array. You can still use `form_for`, but you will need do more configuration in your controller – Gerry May 14 '17 at 13:42
  • Thanks for your comment. Does form_tag more effective in this case? – PrepareFor May 14 '17 at 13:44
  • Yes, i prefer `form_tag` (for this specific case) since is a generic tag, which allows to send params as an array. At the end both are just helpers that will render out a ´form´, so either one will work. `form_for` is simpler for more standard (i.e. 1 object) scenarios. – Gerry May 14 '17 at 13:46
  • I really appreciate your kind reply!! i'll try! Thanks – PrepareFor May 14 '17 at 13:59

0 Answers0