HABTM & collection_ids: confirm changes before saving

16 views
Skip to first unread message

MaggotChild

unread,
Dec 31, 2009, 2:08:30 PM12/31/09
to Ruby on Rails: Talk
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?

MaggotChild

unread,
Dec 31, 2009, 2:10:07 PM12/31/09
to Ruby on Rails: Talk

On Dec 31, 11:08 am, MaggotChild <hsomob1...@yahoo.com> wrote:
> def preview
>   @product = Product.find params[:id]
>   @product.attributes = params[:product]  #no product

I meant that no provider_ids are included in params

Reply all
Reply to author
Forward
0 new messages