Forum: Ruby on Rails Update collection passed to form_tag

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.
Supraja (Guest)
on 2008-11-19 18:43
(Received via mailing list)
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
Frederick C. (Guest)
on 2008-11-19 19:09
(Received via mailing list)
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
Supraja (Guest)
on 2008-11-19 23:39
(Received via mailing list)
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

----

* 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) %>
    <% end %>
    </p>
  <% end %>
  <p>
      <%= submit_tag "Update" %>
  </p>
<% end %>





On Nov 19, 11:07 am, Frederick C. <removed_email_address@domain.invalid>
Supraja (Guest)
on 2008-11-21 01:32
(Received via mailing list)
Hi everyone,

Thanks to Fred for an excellent explanation of params in his blog
post: http://www.spacevatican.org/2008/7/18/parametrised....
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
This topic is locked and can not be replied to.