[Rails] Retain form data on redirection

1,989 views
Skip to first unread message

Hemant Bhargava

unread,
Apr 29, 2010, 7:17:10 AM4/29/10
to rubyonra...@googlegroups.com
Guyz,

How to retain form data while redirecting back to same page? i do not
want to work with sessions option. Any other way ..? I mean i am
creating a record and after hitting submit button one of my validation
failed and the form redirects to the same page where i have to fill the
form values again. But this time every text field value are gone..

Googled but no light.. :)
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Robert Walker

unread,
Apr 29, 2010, 9:00:05 AM4/29/10
to rubyonra...@googlegroups.com
Hemant Bhargava wrote:
> Guyz,
>
> How to retain form data while redirecting back to same page? i do not
> want to work with sessions option. Any other way ..? I mean i am
> creating a record and after hitting submit button one of my validation
> failed and the form redirects to the same page where i have to fill the
> form values again. But this time every text field value are gone..

Controllers typically do not redirect upon validation failures. If you
simply render the page again your form data will be available and the
Rails framework will kindly repopulate the form fields for you
automatically.

If you want to know what that code looks like just run a scaffold
generator in new Rails project. Follow that pattern in your own
controllers and you'll save yourself a lot of headaches.

That being said... a redirect is going to lose the context of the
previous request. HTTP is a stateless protocol. This is the reason
cookies and sessions exist, and a primary reason why frameworks, such as
Rails, are needed.

Hemant Bhargava

unread,
Apr 29, 2010, 10:57:03 AM4/29/10
to rubyonra...@googlegroups.com
Robert Walker wrote:
> Hemant Bhargava wrote:
>> Guyz,

> Controllers typically do not redirect upon validation failures. If you
> simply render the page again your form data will be available and the
> Rails framework will kindly repopulate the form fields for you
> automatically.
>
> If you want to know what that code looks like just run a scaffold
> generator in new Rails project. Follow that pattern in your own
> controllers and you'll save yourself a lot of headaches.
>
> That being said... a redirect is going to lose the context of the
> previous request. HTTP is a stateless protocol. This is the reason
> cookies and sessions exist, and a primary reason why frameworks, such as
> Rails, are needed.

Thanks dude.. Thanks for such a good information to me.. :) I made it
worked as well.

Pix

unread,
May 14, 2010, 8:15:48 AM5/14/10
to Ruby on Rails: Talk
I have a view that is a form in add.html.erb and in the create method
in the controller I have validation check if everything in the form is
set. If validation finds an error, I do

render :action => :add

and all the set values are lost.

if I just do "render" or nothing, it defaults to the create view.
Obviously redirect to add didn't work either.

More suggestions?

David

unread,
May 14, 2010, 3:08:32 PM5/14/10
to Ruby on Rails: Talk
You may have to supply some code, as render :action => :add should
work. Do the variables have the same names? Are there any variables
set in the "add" action that aren't getting set in the "create"
action? Rendering an action does not run its controller code, only
renders its view template.

Pix

unread,
May 17, 2010, 9:18:22 AM5/17/10
to Ruby on Rails: Talk
I set a cookie but otherwise don't really have any variables that
cross. I also have other forms that are even simpler (not even the
cookie) and they blank out as well. I also checked my views, no
variables are the same. Thanks for your help.


class Qcsubmissions::SetupDatabasesController < ApplicationController
layout 'qcsubmissions'

def create
if params[:setup_database]['page'] == "index"
#Set variables and set cookies
sCode = params[:setup_database]['DatabaseCode']
cookies[:code] = sCode
#determine if code is queriable or not
db = SetupDatabase.new(sCode, current_user.name)
bExist = db.newDatabase()
if bExist then
redirect_to :action => :query
else
redirect_to :action => :add
end
#Add new, Product and Action Tables
elsif params[:setup_database]['page'] == "add"
#Set variables and set cookies
sCode = params[:setup_database]['DatabaseCode']
cookies[:code] = sCode
#determine if code is queriable or not
db = SetupDatabase.new(sCode, current_user.name)
bExist = db.newDatabase()
if bExist == false
if validateProductTable(params[:setup_database],
params[:dbType], params[:interface])
db.addProduct(params[:setup_database]['DatabaseName'],
params[:dbType], params[:interface])
redirect_to :action => :query
db.emailSD(current_user.id)
else
render :action => :add
end
else
render :action => :add
end
else
render :action => :add
end
else
flash[:error] = "<div>Submission Not Accepted: Database
already exists.</div>"
redirect_to :action => :query
end
end
end

def query
#variables to pass to the view
@code = cookies[:code]
@db = "development"
end

def add
#variables to pass to the view
@code = cookies[:code]
end

def index
end

def show
redirect_to :action => :index
end

#Validate Insert into Product Table
def validateProductTable(formParams, formType, interface)
bOK = true
fmessage = Array.new
if formParams['DatabaseName'].blank?
bOK = false
fmessage << "<div>Database Name is required.</div>"
end
if formParams['DatabaseCode'].blank?
bOK = false
fmessage << "<div>Database Name is required.</div>"
end
if formType.blank?
bOK = false
fmessage << "<div>Database Type must be selected.</div>"
end
if interface.blank?
bOK = false
fmessage << "<div>Interface must be selected.</div>"
end
if fmessage.length > 0
flash[:error] = fmessage
end
return bOK
end
end
Reply all
Reply to author
Forward
0 new messages