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:

  1. render 'thanks' template directly(not action #thanks)

    render 'thanks' # template 

    you can send whatever instance variable template directly. #thanks no longer needed in case.

    drawback: url won't changed.

  2. convey messages through session

    if want show messages, can prepare in #create , send through session or flash(part of session actually). flash better don't need clear manually.

    note: may want use activerecord session storage if message size big, otherwise you'll meet cookiesoverflow default setting.

  3. 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

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -