23 Jul 12:33
Object saved before validated?
From: Heinz Strunk <rails-mailing-list@...>
Subject: Object saved before validated?
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2008-07-23 10:35:31 GMT
Subject: Object saved before validated?
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2008-07-23 10:35:31 GMT
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:
(Continue reading)
RSS Feed