[Rails] multiple input record, single model (without nested model)

33 views
Skip to first unread message

just-a-noob

unread,
Jul 4, 2011, 2:48:09 AM7/4/11
to rubyonra...@googlegroups.com

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 

<% 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 :)

Walter Lee Davis

unread,
Jul 4, 2011, 12:39:43 PM7/4/11
to rubyonra...@googlegroups.com

On Jul 4, 2011, at 2:48 AM, just-a-noob wrote:

> 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.

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

>
> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aA4013ayyooJ
> .
> To post to this group, send email to rubyonrails-
> ta...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
> .

Reply all
Reply to author
Forward
0 new messages