# Do form helpers work with iteration?

**URL:** <https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747>\
**Category:** Rails\
**Created:** [November 24, 2005, 1:59am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747 "2005-11-24T01:59:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 1:59am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/1 "2005-11-24T01:59:25Z")

</div>

I’m trying to do:

\<% for item in @items %\>  
…  
\<%= text\_field(‘item[]’, ‘title’) %\>  
\<%= check\_box(‘item[]’, ‘active’) %\>  
…  
\<% end %\>

Possible? I get a nil object error.

thanks  
csn

* * *

Yahoo! Music Unlimited  
Access over 1 million songs. Try it free.  
[http://music.yahoo.com/unlimited/](http://music.yahoo.com/unlimited/)

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 2:19am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/2 "2005-11-24T02:19:31Z")

</div>

try:

\<% for item in @items %\>  
…  
\<%= text\_field(item, title) %\>  
\<%= check\_box(item, active) %\>  
…  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 2:47am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/3 "2005-11-24T02:47:41Z")

</div>

Results:

\<%= text\_field(item, title) %\>  
undefined local variable or method `title’ for  
#\<#Class:0x40757350:0x40757288\>

\<%= text\_field(item, ‘title’) %\>  
`@#Item:0x4099796c’ is not allowed as an instance variable name

csn

— Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> > 
> > * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Yahoo! Music Unlimited  
Access over 1 million songs. Try it free.  
[http://music.yahoo.com/unlimited/](http://music.yahoo.com/unlimited/)

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 4:36am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/4 "2005-11-24T04:36:23Z")

</div>

Is there any reason why a for loop is preferable to an each iterator?  
From  
what I’ve seen ruby prefers the iterator approach. Is this correct or  
have  
I misunderstood? I don’t know where I’ve got this impression from, but  
I  
would like to know which is better.

Cheers

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 3:04am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/5 "2005-11-24T03:04:58Z")

</div>

> \<% for item in @items %\>  
> …  
> \<%= text\_field(‘item[]’, ‘title’) %\>  
> \<%= check\_box(‘item[]’, ‘active’) %\>  
> …  
> \<% end %\>
> 
> Possible? I get a nil object error.

i do something similar in an application, sorry for not replacing the  
instant variable names with something more generic, but i’m sure you  
will get the idea.

\<% for @question in @course.questions -%\>  
\<%= text\_field(“question[]”, ‘question’ -%\>  
  
\<% for @answer in @question.answers -%\>  
\<%= text\_field(“answer[]”, ‘answer’, -%\>  
  
\<%= check\_box(“answer[]”, ‘correct’, -%\>  
  
\<% end -%\>  
\<% end -%\>

hope that helps.

wait. it looks like this is what you are trying to do… if so, just  
make sure that you have something in @items.

-ian

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 5:12am UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/6 "2005-11-24T05:12:35Z")

</div>

Liquid wrote:

> Is there any reason why a for loop is preferable to an each iterator?  
> From what I’ve seen ruby prefers the iterator approach. Is this  
> correct or have I misunderstood? I don’t know where I’ve got this  
> impression from, but I would like to know which is better.

As far as I know,  
for v in values … end

is identical to

values.each do |v| …end

I prefer the internal iterator approach, for loops in ruby seem to be  
nothing more than syntactic sugar and sugar is bad for you 😉

Kev

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 24, 2005, 1:25pm UTC](https://www.ruby-forum.com/t/do-form-helpers-work-with-iteration/48747/7 "2005-11-24T13:25:09Z")

</div>

> As far as I know,  
> for v in values … end
> 
> is identical to
> 
> values.each do |v| …end
> 
> I prefer the internal iterator approach, for loops in ruby seem to be  
> nothing more than syntactic sugar and sugar is bad for you 😉

yep. i could just have easily done this:

\<% @course.questions.each do |q| -%\>  
\<%= text\_field(“question[]”, ‘question’ -%\>  
  
\<% @question.answers.each do |a| -%\>  
\<%= text\_field(“answer[]”, ‘answer’, -%\>  
  
\<%= check\_box(“answer[]”, ‘correct’, -%\>  
  
\<% end -%\>  
\<% end -%\>

ian
