How to write Array as html?


#1

Hi everyone,

I’m trying to create a dynamic set of text fields as follows:

<% for i in 0…count%>
<input id="<%=“notes_#{i}”%>" name="<%=“notes[#{i}]”%>" type=“text”
size=“65” maxlength=“55” value=""/>
<%end%>

In the controller, I grab the contents of params[:notes] and loop
through
them. From what I see, :notes is not an Array but a Hash (ex:
“1”=>“test1”,
“3”=>“test3”, “2”=>“test2”)

How do I write notes in rhtml as an array and not a hash? I need to
preserve
order.

Thanks!
Jin


#2

Hi Jin,

Jin L. wrote:

I’m trying to create a dynamic set of text fields as follows:

In the controller, I grab the contents of params[:notes]
and loop through them.

I don’t think I’m understanding your question.

The params hash is created by the browser and passed to the server. In
Rails that means it’s received by an action in a controller.

If you want to create an array from the params hash, assuming from your
example that the keys to the hash are translatable to integers, you can
do:

notes_array = Array.new
params[:notes].each {|key, value| notes_array[key] = value}

If, on the other hand, you want to pass an array to a view for Rails to
use
in creating a new page, you’d create an instance variable of type Array
in
your controller and then reference it in the view (.rhtml) file. If by
“preserve order” you mean you need to render the text boxes visually
from
top to bottom by index, you’ll want to iterate through the array (e.g.,
@notes.each) in your view and render a partial for each element.

hth,
Bill