# How to restore fields ("validates\_presence\_of")

**URL:** <https://www.ruby-forum.com/t/how-to-restore-fields-validates-presence-of/103610>\
**Category:** Rails\
**Created:** [May 8, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/how-to-restore-fields-validates-presence-of/103610 "2007-05-08T13:10:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 8, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/how-to-restore-fields-validates-presence-of/103610/1 "2007-05-08T13:10:22Z")

</div>

Hi all,  
I am using “validates\_presence\_of” for validating all fields of my  
registration form. It is working fine and generating custom error  
message as mentioned.  
Also clearing all fields. How can restore those fields which are already  
filled.  
Problem is “validates\_presence\_of” clearing (blank) field if it fails,  
instead of restoring fields which are already filled.  
Help.

Thanks

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 9, 2007, 3:05am UTC](https://www.ruby-forum.com/t/how-to-restore-fields-validates-presence-of/103610/2 "2007-05-09T03:05:19Z")

</div>

I believe reloading fields is handled in the controller action for  
create, scaffolding builds a create method like:  
create  
@request = Request.new(params[:request])  
if @request.save  
flash[:notice] = ‘Request was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

the line  
@request = Request.new(params[:request])  
should reload the submitted values.

Seth

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 10, 2007, 10:42am UTC](https://www.ruby-forum.com/t/how-to-restore-fields-validates-presence-of/103610/3 "2007-05-10T10:42:15Z")

</div>

> create, scaffolding builds a create method like:  
> create  
> @request = Request.new(params[:request])  
> if @request.save  
> flash[:notice] = ‘Request was successfully created.’  
> redirect\_to :action =\> ‘list’  
> else  
> render :action =\> ‘new’  
> end  
> end
> 
> the line  
> @request = Request.new(params[:request])  
> should reload the submitted values.

Yes, I am doing in same way, but can not happen. User has to again, fill  
all fields, Please suggest if any thing wrong goes.  
my controller  
##############  
def create  
#@met = Met.new(@params[‘met’])  
@met = Met.new(params[:met])  
if @met.save  
session[:user] = @met.id  
redirect\_to :controller =\>‘project’, :action =\> ‘proj’, :id =\>  
@met.id  
else  
render\_action ‘new’  
end  
end  
########################

Thanks,

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 10, 2007, 11:10am UTC](https://www.ruby-forum.com/t/how-to-restore-fields-validates-presence-of/103610/4 "2007-05-10T11:10:40Z")

</div>

> def create  
> #@met = Met.new(@params[‘met’])  
> @met = Met.new(params[:met])  
> if @met.save  
> session[:user] = @met.id  
> redirect\_to :controller =\>‘project’, :action =\> ‘proj’, :id =\>  
> @met.id  
> else  
> render\_action ‘new’  
> end  
> end  
> ########################
> 
> Thanks,

Same code works for update module, and restore the fields.  
########################  
def update  
@met = Met.find(params[:id])  
@met.save  
if @met.update\_attributes(params[:met])  
redirect\_to :controller =\>‘project’, :action =\> ‘proj’, :id =\>  
@met.id  
else  
render\_action ‘edit’  
end  
end  
##############################  
But not for create ,  
Please help me out.

Thanks,
