views:

184

answers:

1

I want to take input from a textfield and turn it into an array of strings. After having submitted the post request, I want to display again the textfield, but including the values of the textfield in an array.

I have a view that would look like:

<% form_tag "/list2array" do -%>
  <%= text_area_tag "mylist" %>
<div><%= submit_tag 'save' %></div>
<% end -%>


<% @myArray.each do |item| %>
    <%= item %>
<% end %>

And as a start for the controller:

class List2ArrayController < ApplicationController  
  def index
  end

  def save
   @myArray = params[:mylist].split("\r\n")
  end

end

However, after the post, I only get an empty textfield without values in the array from the previous POST.

Do I need to use the model layer for my experiment? How? Or do I need to modify my controller?

+1  A: 

Sort answer: Yes. You need to use some form of data store, either models or you can store it in the session. The are no continuations of state.

If you have a model you can add an attribute called mylist and mylist_array (you can use serialize for the array). Then either with a setter, or a before_validations callback set the value of mylist_array as you do in your example.

On a slightly contradictory note: adding the following to the end of your save method, will make your experiment sort of work, but you will need to fix your form post url first or add in a route for it manually.

render :index 
MatthewFord
thanks for the pointer. actually I want to be able to insert a bulk or batch of records via one textfield.did not find a solution for this now, but this is already some form: http://asciicasts.com/episodes/198-edit-multiple-individuallyIndeed, some understanding of REST routing is necessary.
poseid
The Rails guides are a good resource, this might help: http://guides.rubyonrails.org/form_helpers.html#building-complex-forms
MatthewFord