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