Hi everyone,
Thanks to Fred for an excellent explanation of params in his blog
post: 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 %>
<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
On Nov 19, 3:39 pm, Supraja <[EMAIL PROTECTED]> wrote:
> 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 Cheung <[EMAIL PROTECTED]>
> wrote:
>
> > On 19 Nov 2008, at 16:25, Supraja wrote:
>
> > > Hi everyone,
>
> > > I'm trying to update attributes in acollectionpassed 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
>
> > > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---