Do form helpers work with iteration?


#1

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/


#2

try:

<% for item in @items %>

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

<% end %>


#3

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

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


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


#4

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


#5

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


#6

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


#7

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