Rails - how to pass created record from the new form to a redirected page -
i think pretty simple question nothing i've read has answered question directly:
i have new products page standard form. after submitting form, redirect custom controller action , view called "thanks".
on "thanks" page, want able print name of product created , possibly other attributes.
how pass object created new action? right controller looks this:
def create @product = product.new(params[:product]) if @product.save flash[:notice] = "successfully created product." redirect_to thanks_path else render :action => 'new' end end def end
you can't send object through redirect.
there 3 ways solve problem:
render 'thanks' template directly(not action #thanks)
render 'thanks' # templateyou can send whatever instance variable template directly.
#thanksno longer needed in case.drawback: url won't changed.
convey messages through session
if want show messages, can prepare in
#create, send throughsessionorflash(part of session actually).flashbetter don't need clear manually.note: may want use activerecord session storage if message size big, otherwise you'll meet cookiesoverflow default setting.
send simple message through session obj_id
similar #2 thinks better #2. in
#thanks, can construct complex message according if obj_id present, id , find related data through db.
Comments
Post a Comment