Do form helpers work with iteration?

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/

try:

<% for item in @items %>

<%= text_field(item, title) %>
<%= check_box(item, active) %>

<% end %>

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. [email protected] wrote:

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


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

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

<% 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

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 :wink:

Kev

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 :wink:

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