Object saved before validated?

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs