Hey,
I''m having a weird problem: When I add a product via webform
it''s being
saved before validated.
Example: I fill out the form for a product named "Test", I forget to
type in the price so the error "Price can''t be blank" and it
returns to
the form. After I typed in a price it says "Product
''Test'' already
exists" because it''s been saved the first time already.
Controller:
def create_product
Product.prepare_to_save(params[:product])
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
flash[:notice] = ''Product was successfully created.''
format.html { redirect_to :action => ''index'' }
format.xml { render :xml => @product, :status => :created,
:location => @product }
else
format.html { render :action => "new_product" }
format.xml { render :xml => @product.errors, :status =>
:unprocessable_entity }
end
end
end
Model:
..
validates_presence_of :title, :description, :image, :category
validates_numericality_of :price
validates_uniqueness_of :title
..
Has anyone an idea why this is happening?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---