This is simple (I am sure) but how do you get the id of the inserted element after the element has been save? I thought it was something like this: @product = Product.new(params[:product] if @product.save add_image(product.id, params["image"]) flash[:notice] = ''Product was successfully created.'' redirect_to :action => ''list_products'' end But that isn''t it -- Posted via http://www.ruby-forum.com/.
i think the reference to product in add_image(product_id... is missing ''@'' to reference the instance variable, not the a local variable. so it would be add_image(@product.id, params[:product]) I could be wrong, but try it out. On 3/4/06, Seth Buntin <seth.buntin@coe.murraystate.edu> wrote:> > This is simple (I am sure) but how do you get the id of the inserted > element after the element has been save? > > I thought it was something like this: > > @product = Product.new(params[:product] > > if @product.save > add_image(product.id, params["image"]) > flash[:notice] = ''Product was successfully created.'' > redirect_to :action => ''list_products'' > end > > But that isn''t it > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060304/27b0c568/attachment.html
You should be able to reference product as "@product.id" instead of "product.id". If that doesn''t work, do "@product.reload", and you should then have the "id". On 3/4/06, Seth Buntin <seth.buntin@coe.murraystate.edu> wrote:> This is simple (I am sure) but how do you get the id of the inserted > element after the element has been save? > > I thought it was something like this: > > @product = Product.new(params[:product] > > if @product.save > add_image(product.id, params["image"]) > flash[:notice] = ''Product was successfully created.'' > redirect_to :action => ''list_products'' > end > > But that isn''t it > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I get a nil error when I do this. Even after the @product.reload. Michael Gorsuch wrote:> You should be able to reference product as "@product.id" instead of > "product.id". > > If that doesn''t work, do "@product.reload", and you should then have the > "id".-- Posted via http://www.ruby-forum.com/.
And you''ve confirmed that you''re calling "@product.id" and not "product.id"? On 3/4/06, Seth Buntin <seth.buntin@coe.murraystate.edu> wrote:> I get a nil error when I do this. Even after the @product.reload. > > Michael Gorsuch wrote: > > You should be able to reference product as "@product.id" instead of > > "product.id". > > > > If that doesn''t work, do "@product.reload", and you should then have the > > "id". > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
And... are you sure that params["image"] isn''t nil? On 3/4/06, Michael Gorsuch <michael.gorsuch@gmail.com> wrote:> And you''ve confirmed that you''re calling "@product.id" and not "product.id"? > > On 3/4/06, Seth Buntin <seth.buntin@coe.murraystate.edu> wrote: > > I get a nil error when I do this. Even after the @product.reload. > > > > Michael Gorsuch wrote: > > > You should be able to reference product as "@product.id" instead of > > > "product.id". > > > > > > If that doesn''t work, do "@product.reload", and you should then have the > > > "id". > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Michael Gorsuch wrote:> And... are you sure that params["image"] isn''t nil?Sorry. You were right. -- Posted via http://www.ruby-forum.com/.
Not a problem at all - I''ve run into this before. On 3/4/06, Seth Buntin <seth.buntin@coe.murraystate.edu> wrote:> Michael Gorsuch wrote: > > And... are you sure that params["image"] isn''t nil? > > Sorry. You were right. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >