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
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.