I assume a product has_one picture. I believe the following should do
what you want:
def create
@product = Product.new(params[:product])
@product.picture = Picture.new(@params[:picture])
if @product.save
redirect_to :action => ''products''
else
render_action ''new''
end
end
Tom
On 9/19/05, neale <neale-UA3QSAWMS8/0CCvOHzKKcA@public.gmane.org>
wrote:> Hi
>
> I''m new to Rails and I''m struggling with a
''has-one'' relationship. I''m
> trying to save product and image data in seperate tables with one to one
> relationship. I''m assuming the problem lies in the controller
method
> below but can''t work it out. The models contain the has_one and
belongs
> to statements, and I tried swapping these without effect. Data is in
> fact saved in both tables sucessfully but the foreign key (product_id)
> in the pictures table is not saved.
>
> Anybody care to shine a little illumination this way?
>
> def create
> @product = Product.new(params[:product])
> if @product.save
> @picture = Picture.new(@params[:picture])
> if @picture.save
> redirect_to :action => ''products''
> else
> render_action ''new''
> end
> else
> render_action ''new''
> end
> end
>
> cheers
>
> Neale
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>