Update collection passed to form_tag

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

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.

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

Hi Fred,

I appreciate your quick reply.

Here's an example of an HTML tag after I used fields_for() (code pasted below*):

<input id="candle_flavor" name="candle[flavor]" size="30" type="text" value="Vanilla" />

How can I change edit_all.html.erb code to get:

id="candle[id][flavor]" name="candle[id][flavor]"

Thanks, Supraja

Hi everyone,

Thanks to Fred for an excellent explanation of params in his blog post: Parametrised to the max - Space Vatican. 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 %>     <p>     <% fields_for @candle do |can| %>       <%= can.text_field(:flavor, "index" => @candle.id) %>     <% end %>     </p>   <% end %>   <p>       <%= submit_tag "Update" %>   </p> <% 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