# Update collection passed to form\_tag

**URL:** <https://www.ruby-forum.com/t/update-collection-passed-to-form-tag/153638>\
**Category:** Rails\
**Created:** [November 19, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/update-collection-passed-to-form-tag/153638 "2008-11-19T17:43:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Supraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@Supraja](https://www.ruby-forum.com/u/Supraja)\
**Post date:** [November 19, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/update-collection-passed-to-form-tag/153638/1 "2008-11-19T17:43:56Z")

</div>

Hi everyone,

I’m trying to update attributes in a collection passed to form\_tag.  
However my syntax for extracting values from the params hash results  
in this exception:

undefined method `keys’ for [“Vanilla”, “Rose”, “Cranberry”]:Array

So it appears I am trying to call keys on an array of “flavor”  
attribute values, rather than extracting “Candle” object id keys from  
the params hash. I have included the code from the view and controller  
below.

Thanks for taking the time to read this. I appreciate any specific  
suggestions, as well as any resources that explain the intricacies of  
params.

Thank You!  
Supraja

* * *

edit\_all.html.erb:  
\<% form\_tag(:action =\> “update\_candle\_line”) do %\>  
\<% for @candle in @candles %\>  
\<%= text\_field\_tag(“candle[]”, @candle.flavor) %\>  
\<% end %\>

\<%= submit\_tag “Update” %\>  
\<% end %\>

* * *

edit\_all\_controller.rb:  
def update\_candle\_line  
Candle.update(params[:candle].keys, params[:candle].values)  
redirect\_to\_index(“Flavors updated!”) # private method of  
controller  
end

---

<div class="post-metadata">

**Author:** ![Supraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@Supraja](https://www.ruby-forum.com/u/Supraja)\
**Post date:** [November 19, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/update-collection-passed-to-form-tag/153638/2 "2008-11-19T18:09:47Z")

</div>

On 19 Nov 2008, at 16:25, Supraja wrote:

> attribute values, rather than extracting “Candle” object id keys from  
> the params hash. I have included the code from the view and controller  
> below.

If you call your parameters candle[] then you will indeed get back a  
single array parameter

if you wanted a hash keyed by id then you’d need parameters of the  
form candle[some\_id][flavor]. fields\_for etc… may be able to help  
you create those parameter names rather than having to bash them in by  
hand.

Fred

---

<div class="post-metadata">

**Author:** ![Supraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@Supraja](https://www.ruby-forum.com/u/Supraja)\
**Post date:** [November 19, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/update-collection-passed-to-form-tag/153638/3 "2008-11-19T22:39:33Z")

</div>

Hi Fred,

I appreciate your quick reply.

Here’s an example of an HTML tag after I used fields\_for() (code  
pasted below\*):

How can I change edit\_all.html.erb code to get:

id=“candle[id][flavor]” name=“candle[id][flavor]”

Thanks,  
Supraja

* * *

- edit\_all.html.erb  
\<% form\_tag(:action =\> “update\_candle\_line”) do %\>

\<% end %\>

On Nov 19, 11:07 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Supraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@Supraja](https://www.ruby-forum.com/u/Supraja)\
**Post date:** [November 21, 2008, 12:32am UTC](https://www.ruby-forum.com/t/update-collection-passed-to-form-tag/153638/4 "2008-11-21T00:32:08Z")

</div>

Hi everyone,

Thanks to Fred for an excellent explanation of params in his blog  
post: [Parametrised to the max - Space Vatican](http://www.spacevatican.org/2008/7/18/parametrised-to-the-max).  
Anyone who is interested in params could probably learn a lot from  
reading this interactive post.

Based on the post, I was able to update\_attributes() to a collection  
of candles.

Thank You!  
Supraja

# edit\_all.html.erb

\<% form\_tag(:action =\> “update\_candle\_line”) do %\>

\<% for @candle in @candles %\>

\<% fields\_for @candle do |can| %\>  
\<%= can.text\_field(:flavor, “index” =\> @candle.id) %\>  
\<% end %\>

  
\<% end %\>

\<%= submit\_tag "Update" %\>

\<% end %\>
# edit\_all\_controller.rb

def update\_candle\_line  
params[:candle].each do |id, new\_attributes|  
Candle.find(id).update\_attributes new\_attributes  
end  
redirect\_to\_index(“Flavors updated!”)  
end
