sorry to bother to help me again.I invoke create action using ajax.The
variant and it’s associations are put in a transaction.When both are
saved successfully,render create_variant using ajax.When anyone
unsuccessfully,rallback all the saving manipulation,rescue exception,and
render the new_variant using ajax,which has <%=error_messages_for
:variant-%> to display invalidations.
But when inputtings are invalid,i always get something like
attachment.It seems the exception is not handled at all,the error
meassages are not displayed as expected.can you help me? thank!
###############
def create
@product=Product.find_by_id(params[:product_id])
@variant=Variant.new(params[:variant])
@product.variants<<@variant
@base_option=Option.new(:name=>params[:base_option][:name],:price=>0)
@variant.options<<@base_option
Variant.transaction do
begin
@variant.options.each{|option|option.save!}
@variant.save!
flash.now[:notice]=‘variant created successfully’
render :action=>:create_variant,:layout=>false
rescue Exception
raise
render :action=>:new_variant,:layout=>false
end
end
end
##############
hi
can you help me?
I tested using non-ajax call,but the problem was same,the exception was
not handled like the attachment and new_variant template was not
rendered as expected.The render should not be used in the rescue block?
can you help me?
On Aug 2, 4:12 am, Guo Y. [email protected] wrote:
I tested using non-ajax call,but the problem was same,the exception was
not handled like the attachment and new_variant template was not
rendered as expected.The render should not be used in the rescue block?
can you help me?
You’re rescuing the exception but then you’re re-raising it so your
render call never happens. Also a more usual way is to call save and
test the return value of that rather than call save! and rescue the
exception
Fred
On Aug 2, 1:11 pm, Guo Y. [email protected] wrote:
You’re rescuing the exception but then you’re re-raising it so your
render call never happens. Also a more usual way is to call save and
test the return value of that rather than call save! and rescue the
exceptionThanks fred.But i want transaction(which need save!) to let them all
either to be saved or not.If i rescue the exception and not re-raising
it,is it right to handle exception in this way?
in that case you should rescue the exception outside of the
transaction
Fred
Thank you for your patience,fred.
You’re rescuing the exception but then you’re re-raising it so your
render call never happens. Also a more usual way is to call save and
test the return value of that rather than call save! and rescue the
exception
Thanks fred.But i want transaction(which need save!) to let them all
either to be saved or not.If i rescue the exception and not re-raising
it,is it right to handle exception in this way?