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?