Forum: Ruby on Rails Do form helpers work with iteration?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-24 01:59
(Received via mailing list)
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/
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-11-24 02:19
(Received via mailing list)
try:

<% for item in @items %>
...
<%= text_field(item, title) %>
<%= check_box(item, active) %>
...
<% end %>
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 cool_screen_name90001 (Guest)
on 2005-11-24 02:47
(Received via mailing list)
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 Hall <christopher.k.hall@gmail.com> wrote:

> >
> >
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> > _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>




__________________________________
Yahoo! Music Unlimited
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/
8e4f193e6765aaa57925baa2876f55a7?d=identicon&s=25 Ian Kennedy (slip)
on 2005-11-24 03:04
> <% 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' -%><br/>
    <% for @answer in @question.answers -%>
      <%= text_field("answer[]", 'answer', -%><br/>
      <%= check_box("answer[]", 'correct', -%><br/>
    <% 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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-11-24 04:36
(Received via mailing list)
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
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-11-24 05:12
(Received via mailing list)
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
8e4f193e6765aaa57925baa2876f55a7?d=identicon&s=25 Ian Kennedy (slip)
on 2005-11-24 13:25
> 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' -%><br/>
    <% @question.answers.each do |a| -%>
      <%= text_field("answer[]", 'answer', -%><br/>
      <%= check_box("answer[]", 'correct', -%><br/>
    <% end -%>
<% end -%>

ian
This topic is locked and can not be replied to.