# Basic question about form\_for

**URL:** <https://www.ruby-forum.com/t/basic-question-about-form-for/86287>\
**Category:** Rails\
**Created:** [February 21, 2007, 4:30am UTC](https://www.ruby-forum.com/t/basic-question-about-form-for/86287 "2007-02-21T04:30:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 21, 2007, 4:30am UTC](https://www.ruby-forum.com/t/basic-question-about-form-for/86287/1 "2007-02-21T04:30:44Z")

</div>

The form\_for api docs have this example:

\<% form\_for :person, @person, :url =\> { :action =\> “update” } do |f| %\>

Can someone explain the function of :person and @person? I guess that  
@person was set in the controller.  
But then what is :person? Why do I need both?

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 21, 2007, 4:54am UTC](https://www.ruby-forum.com/t/basic-question-about-form-for/86287/2 "2007-02-21T04:54:45Z")

</div>

> The form\_for api docs have this example:
> 
> \<% form\_for :person, @person, :url =\> { :action =\> “update” } do |f| %\>
> 
> Can someone explain the function of :person and @person? I guess that @person was set in the controller.  
> But then what is :person? Why do I need both?

Try it and then view the source to see the result… using this example:

\<% form\_for :person, @person, :url =\> { :action =\> “update” } do |f| %\>  
First name: \<%= f.text\_field :first\_name %\>  
Last name : \<%= f.text\_field :last\_name %\>  
Biography : \<%= f.text\_area :biography %\>  
Admin? : \<%= f.check\_box :admin %\>  
\<% end %\>

You’d end up with form names of:

person[first\_name]  
person[last\_name]  
person[biography]  
person[admin]

That takes care of :person. @person is what is used to populate those  
fields with default values…

-philip

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 25, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/basic-question-about-form-for/86287/3 "2007-02-25T17:16:12Z")

</div>

Uh… Weird. I’ve been using

form\_for :model\_name, :url =\> {:action =\> :whatever} do |f|  
end

and it works fine. I don’t know how/why I started doing it this way but  
it  
does work. Does Edge Rails use form\_for differently? Is @person designed  
so  
you could use a different model there? I’m genuinely puzzled by this.

RSL

---

<div class="post-metadata">

**Author:** ![suer\_rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@suer\_rick](https://www.ruby-forum.com/u/suer_rick)\
**Post date:** [February 26, 2007, 12:25am UTC](https://www.ruby-forum.com/t/basic-question-about-form-for/86287/4 "2007-02-26T00:25:14Z")

</div>

Russell N. wrote:

> Uh… Weird. I’ve been using
> 
> form\_for :model\_name, :url =\> {:action =\> :whatever} do |f|  
> end
> 
> and it works fine. I don’t know how/why I started doing it this way but  
> it  
> does work. Does Edge Rails use form\_for differently? Is @person designed  
> so  
> you could use a different model there? I’m genuinely puzzled by this.
> 
> RSL

form\_for allows you to use any object, not just an instance variable.  
This allows you to do something like:

#some\_view  
\<% @foobars.each do |fb|  
\<% form\_for :foobar, fb %\>  
\<%= f.text\_field :title %\>  
\<%= submit\_tag ‘Save Foobar!’ %\>  
\<% end %\>  
\<% end %\>

:foobar, is a string or symbol use to name the form fields. So when  
submitted, all form fields will be in params[:foobar]. The second  
argument, fb, is the object whose values will be shown in the form when  
the page loads.

You can leave off the second argument:

\<% form\_for :foo do |f| %\>

And the helper will assume you want an instance variable of the same  
name, so will use @foo for generating the default form values.

So these two lines are the same:

\<% form\_for :foo do |f| %\>  
\<% form\_for :foo, @foo do |f| %\>

But the second allows you to do tricky things like:

\<% form\_for :foo, my\_crazy\_local\_var do |f| %\>
