Forum: Ruby on Rails Single Text_Area for multiple records

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
smfrick (Guest)
on 2008-11-24 13:26
(Received via mailing list)
I have an application I've developed where there is a record of data
which is entered line by line, I've got that working fine. The form
now only has one field, because that's all the data which is
required.   I would like to make it simpler by providing a text_area,
and the user would enter a list of values and on submit have the
controller or the model parse the data into records and append them
into the table.    I've looked for examples of the correct 'Rails way'
of doing this type of process, but everything looks like a single
record posted.  Does anyone know of a Gem or module which will make
this simpler?

Thanks!
Bobnation (Guest)
on 2008-11-24 16:43
(Received via mailing list)
Perhaps, and this is just some "brain dump" at the moment, you could
set up a separate attribute and then use some Ruby to take that imput
and split it so that you can save it as separate attributes. I have no
idea how you would do this code wise, but having a text field set up
as :input and then attr_accesssor :input and then work from there with
some Ruby to split at newlines or with some other kind of operator.
smfrick (Guest)
on 2008-11-24 17:35
(Received via mailing list)
That's a great Idea,  It's the How I'm trying to figure out.  This is
a simple task in PHP or ASP, but Ruby is a different animal
completely, and I don't want to buck the system.  Thanks for your
input.
hoyos (Guest)
on 2008-12-07 11:48
(Received via mailing list)
Hey smfrick,

I just encountered this same problem, here's a rather hacked together
solution, maybe some people here can help optimize it:

def create
  #GRAB FORM DATA
  @temp_array = params[:form_name]
  #GET ARRAY MADE OF EACH LINE OF TEXT_AREA
  @temp_array = @temp_array["text_area"].split(/[\r\n]/)
  #VARIABLE TO ENSURE AT LEAST ONE RECORD WAS SAVED
  @success = 0
  #LOOP
  @temp_array.each do |temp_individual_record|
    if temp_individual_record.length()>0          #ENSURE NO BLANKS
      @model = ModelName.new(params[:form_name])    #GRAB ANY OTHER
FIELDS
      @model.text_area=temp_individual_record      #OVERWRITE TEXTAREA
      @model.save
      @success=1
    end
  end
    respond_to do |format|
      if @success==1
        flash[:notice] = 'Model was successfully created.'
        format.html { redirect_to(@model) }
        format.xml  { render :xml => @model, :status
=> :created, :location => @model }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @model.errors, :status
=> :unprocessable_entity }
      end
    end
  end
This topic is locked and can not be replied to.