Add default value on Insert

The following inserts a new product, no problem. Howeve, I have to
prefix
the filename with “\images” so upon retrieval it will looki in the
right
directory.

How can I modify this code to append “\images” to the filename column?

def create
@product = Product.new(@params[:product])
if @product.save
flash[‘notice’] = ‘Product was successfully created.’
end
end


Check the weather nationwide with MSN Search: Try it now!
http://search.msn.com/results.aspx?q=weather&FORM=WLMTAG

You can define a before_save callback in your model to call a function
which
will prepend “\images”.

On 8/22/06, Jim W. [email protected] wrote:

The following inserts a new product, no problem. Howeve, I have to prefix
the filename with “\images” so upon retrieval it will looki in the right
directory.

How can I modify this code to append “\images” to the filename column?

def create
@product = Product.new(@params[:product])
@product.filename = “\images\” + @product.filename
if @product.save
flash[‘notice’] = ‘Product was successfully created.’
end
end

This approach might be problematic if you’re creating new products in
other
actions or elsewhere in your rails app. Using before_save won’t pose the
same
problem.