Re: Arbitrary sized collections

Peter T. Brown
Sat, 29 Oct 2005 17:05:58 -0000

Actually, what I mean is when I have a form that contains N number of these:

<input id="feed_url" name="feed[url]"
type="hidden"
value="http://images.apple.com/main/rss/hotnews/hotnews.rss" />

<input id="feed_url" name="feed[url]"
type="hidden"
value="http://images.apple.com/main/rss/hotnews/pr.rss" />

....

When this form is posted, what is created is something like this:
{"feed"=>{"url"=>"http://images.apple.com/main/rss/hotnews/hotnews.rss"},
"commit"=>"Create",
"account"=>{"service"=>"jabber",
"action"=>"create",
"controller"=>"feeds"}

So the problem is that I''ve got an arbitrary number of
feed[url]''s that will
be posted. These will be created new, and so do not yet have any Id''s
associated with them.

I can''t figure out how to handle this situation with rails. The form
I''ve
shown above will post something like
feed[url]=blahblah&feed[url]=otherblahthis. Under webwork (to compare) this
would automatically be converted into an ArrayList containing each separate
entry.

(I know that I can manually make input fields that are named feed[1][url],
feed[2][url] etc. But this is a hassle since I don''t know how many of
these
fields there will end up being.)

Is there a way to do this in Ruby on Rails?


Thank You



> From: Shad Reynolds
<shad.reynolds-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> Reply-To:
"rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org"
<rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org>
> Date: Fri, 28 Oct 2005 18:50:21 -0500
> To: "rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org"
<rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org>
> Subject: Re: [Rails] Arbitrary sized collections
>
> Try this:
>
>>          <% for feed in @feeds %>
>>             <%= text_field ''feed'',
''url'' %>
>>          <% end %>
>
> @feed is referencing the instance variable :feed.
>
> You want to create a variable local to the loop (feed).  Then,
> reference that variable as just "feed" (which will contain each
of the
> values in sucsession).
>
> Shad
>
>
> On 10/28/05, Peter T. Brown
<peter-MTMYhAWfEVW8IPqWsmCfhg@public.gmane.org> wrote:
>> I have a list of URL''s I need to associate with a single
Account instance.
>> In my form, the number of URL''s passed in is between 1 and 10.
I do not see
>> how to handle this common situation...
>>
>> Having constructed the Feed model objects that hold the URL and are
>> associated with the Account model I''ve tried:
>>
>>          <% for @feed in @feeds %>
>>             <%= text_field ''feed[]'',
''url'' %>
>>          <% end %>
>>
>> However this simply causes params(:feed) to equal the last item in the
>> list... Since each of these Feed objects is constructed new, none of
them
>> have id''s. I''ve tried associated an incrementing
index doing something like:
>> <%= text_field "feed[#{i}]", ''url'' %>
but I get the error: "`@feed[0]'' is
>> not allowed as an instance variable name"
>>
>>
>> What should I do?
>>
>> Thank You
>>
>>
>> _______________________________________________
>> Rails mailing list
>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
> http://www.ShadReynolds.com
> http://www.flickr.com/photos/shadreynolds/
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails