multiple input record, single model (without nested model)

hello everyone, I’m new to rails i have some question thats been quite a headache to me, so here it is:

for example i have this controller & view

controller


  class OrdersController < ApplicationController
def new
@Order = Order.new
end def create
@Order = Order.new(params[:order])
if @Order.save
flash[:notice] = "Successfully created order."
redirect_to @Order
else
render :action => 'new'
end
end

View


<%= f.submit %>

my question is :
before displaying the form above, I want to have a text_field_tag that specify how many forms (roughly said, duplicate the form div) I want to generate based on count, and then insert the data to the database simultaneously,

This is similar to ryan bates railscasts but they don't meet my requirement, i want to use single model, not nested.

the idea is to speed things up, so that the user don't have to input the data only one at a time, but multiple record at single submit

how do i do that?

Any suggestions and solutions  will be much appreciated

thanks before :)

<% title “Menu Order” %> <%= form_for @Order do |f| %> <%= f.error_messages %>

<%= f.label :name%>
<%= f.text_field :name, %>

<%= f.label :menu_order %>
<%= f.text_field :menu_order %>

```

hello everyone, I'm new to rails i have some question thats been quite a headache to me, so here it is: for example i have this controller & view controller class OrdersController < ApplicationController

  def new   @Order = Order.new   end

  def create     @Order = Order.new(params[:order])     if @Order.save       flash[:notice] = "Successfully created order."       redirect_to @Order     else       render :action => 'new'     end   end

Rails (and Ruby) expect instance variables to begin with a lower-case letter. This may be working for you anyway, but I thought I'd point out that @Order and @order **mean** fundamentally different things.

View

<% title "Menu Order" %> <%= form_for @Order do |f| %>   <%= f.error_messages %>

<div id="form-order">   <p>     <%= f.label :name%><br />     <%= f.text_field :name, %>   </p>   <p>     <%= f.label :menu_order %><br />     <%= f.text_field :menu_order %>   </p> </div>   <%= f.submit %>

my question is : before displaying the form above, I want to have a text_field_tag that specify how many forms (roughly said, duplicate the form div) I want to generate based on count, and then insert the data to the database simultaneously, This is similar to ryan bates railscasts but they don't meet my requirement, i want to use single model, not nested. the idea is to speed things up, so that the user don't have to input the data only one at a time, but multiple record at single submit how do i do that? Any suggestions and solutions will be much appreciated thanks before :slight_smile:

You're going to need to shift your new and create methods around to create and expect an array of orders rather than a single order. This may be very difficult, since it is essentially swimming upstream against Rails' conventions. You might get a lot more mileage out of creating a new wrapper model, like Cart, and having it accept_nested_attributes_for :orders. You can follow the Railscast that way, which works most excellently. (There are a couple of books when you build it in Rails 3, mostly to do with the JavaScript stuff because you'll get double-escaped output if you follow it precisely. Remember that h is implicit in erb templates now, and you have to mark the stuff you don't want escaped with .html_safe.)

Walter