# How to write Array as html?

**URL:** <https://www.ruby-forum.com/t/how-to-write-array-as-html/87323>\
**Category:** Rails\
**Created:** [March 1, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-write-array-as-html/87323 "2007-03-01T20:02:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [March 1, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-write-array-as-html/87323/1 "2007-03-01T20:02:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [March 1, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/how-to-write-array-as-html/87323/2 "2007-03-01T20:42:31Z")

</div>

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
