It seems that when a HABTM (or has many) is involved, having a confirmation page prior to saving doesn''t work as seamlessly as other Rails view/model parings, specifically when confirming an edit. This is caused by the immediate inserts or deletes that take place upon assigning to collection_ids. Changes are saved regardless of whether the user wants to cancel or proceed. select_tag ''product[provider_ids][]'', collection_options_with_selected #pseudo def preview @product = Product.find params[:id] @product.attributes = params[:product] #provider_ids saved here if @product.valid? render ''confirm; else render ''edit'' end end build is not appropriate and push has the same functionality as assigning to provider_ids. So I have to do this: def preview @product = Product.find params[:id] @product.attributes = params[:product] #no product @selected_providers = Provider.find_by_ids params[:provider_ids] #to display provider names in confirm page #... end def update @product = Product.find params[:id] @product.attributes = params[:product] @provider_ids = params[:provider_ids] if @product.valid? Product.transaction do @product.provider_ids = @provider_ids @product.save end else ... end There''s no way to do this in the typical Rails attribute assignment fashion? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
MaggotChild
2009-Dec-31 19:10 UTC
Re: HABTM & collection_ids: confirm changes before saving
On Dec 31, 11:08 am, MaggotChild <hsomob1...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> def preview > @product = Product.find params[:id] > @product.attributes = params[:product] #no productI meant that no provider_ids are included in params -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.