# How does form\_for and validate\_presence\_of work hand in hand

**URL:** <https://www.ruby-forum.com/t/how-does-form-for-and-validate-presence-of-work-hand-in-hand/168797>\
**Category:** Rails\
**Created:** [May 26, 2009, 6:24am UTC](https://www.ruby-forum.com/t/how-does-form-for-and-validate-presence-of-work-hand-in-hand/168797 "2009-05-26T06:24:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 26, 2009, 6:24am UTC](https://www.ruby-forum.com/t/how-does-form-for-and-validate-presence-of-work-hand-in-hand/168797/1 "2009-05-26T06:24:11Z")

</div>

when we have validate\_presence\_of :name in the model and then when we  
put in the create action that we re-render ‘new’, then the form\_for will  
populate the fields, and error\_messages\_for ‘story’ will have the  
correct error message.

this is really great, and and the same time, this looks like magic… i  
found that many books don’t explain how the magic occur. is it by some  
global variable?

when the form\_for is called… is it using the @story that came back  
from the @story.save, instead of the @story = Story.new from the new  
action?

sometimes i feel that i am playing magic when using Ruby on Rails,  
except I don’t know how the magic happens… kind of like if I make the  
rabbit appear, but I don’t know how I did it. So I really want to know  
the inner workings of Rails.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [May 26, 2009, 9:31am UTC](https://www.ruby-forum.com/t/how-does-form-for-and-validate-presence-of-work-hand-in-hand/168797/2 "2009-05-26T09:31:12Z")

</div>

On May 26, 5:24 am, SpringFlowers AutumnMoon \<rails-mailing-  
removed\_email\_address@domain.invalid\> wrote:

> this is really great, and and the same time, this looks like magic… i  
> found that many books don’t explain how the magic occur. is it by some  
> global variable?
> 
> when the form\_for is called… is it using the @story that came back  
> from the @story.save, instead of the @story = Story.new from the new  
> action?

You render the new template, but you don’t run the new action again:  
the @story you display is indeed the one that you tried to save.

Fred
