How to use variables inside rhtml?



I want to use counters in my rhtml page for dynamic naming of my form
eg:- for 1st textbox name will be “text1”
for 2nd textbox name will be “text2”
…& so on…

But here I want use a counter variable in rHtml file so that I will use
for loop for looping througn each set of elements in form & give name to
form elements as
“element_name”+i (i is counter variable)
I tried it with following code but its nt working:-

<% for product in @products %>
<%= text_field ‘product_item’,‘removed_email_address@domain.invalid_s’,

But its not working proper as I m accessing the value of each textbox in
controller class as folllows:-

for product in @products

it gives me error like:-
“’+’ is undefined”

How to deal with this???

Thanx in advance for ur reply.



Prashant T. wrote:

<% for product in @products %>
<%= text_field ‘product_item’,‘removed_email_address@domain.invalid_s’,

Rails has build in support for this.

Use something like:

<% @products.each do |@p| %>
<%= text_field ‘p[]’, … %>
<% end>

This will automatically enumerate your fields in your form.

In your controller using params[:p] you fetch you an array of the values
entered in all the ‘p’ fields.