# Form \_for usage

**URL:** <https://www.ruby-forum.com/t/form-for-usage/138195>\
**Category:** Rails\
**Created:** [May 21, 2008, 12:13am UTC](https://www.ruby-forum.com/t/form-for-usage/138195 "2008-05-21T00:13:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 12:13am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/1 "2008-05-21T00:13:34Z")

</div>

Hi all,

I am sure that this is documented somewhere, but I can’t find it. I  
have a class A that has references class B. Like, a.b.name. I was  
wondering how to represent that with form\_for?

\<% form\_for(@a) do |f| %\>  
\<%= f.text\_field “b.name” %\>  
\<% end %\>

That doesn’t work, but it kinda shows you what I am talking about.

Any help at all would be appreciated. Thanks!

-fREW

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 12:29am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/2 "2008-05-21T00:29:59Z")

</div>

So I’m assuming each instance of class A only references one instance  
of class B. If not, this answer changes, but if that’s the case just  
let me know.

In the model A.rb:  
belongs\_to :b

In the model B.rb:  
has\_many :as # or belongs\_to :a if the relationship is one-to-one

In the migration for A:  
t.integer :b\_id

Then everything should be kosher.

ESPNDev

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 12:34am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/3 "2008-05-21T00:34:04Z")

</div>

> In the model A.rb:  
> belongs\_to :b
> 
> In the model B.rb:  
> has\_many :as # or belongs\_to :a if the relationship is one-to-one
> 
> In the migration for A:  
> t.integer :b\_id

Well, that’s all correct. But I already have the model and migrations  
defined. I just want to know how to set up a form with form\_for to  
set values for a.b.blah.

-fREW

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 12:51am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/4 "2008-05-21T00:51:03Z")

</div>

Oh my bad. You need to use

\<%= text\_field\_tag “b”, @a.b.name %\>

And then you’ll be able to access stuff with params[:b]

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 1:36am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/5 "2008-05-21T01:36:45Z")

</div>

wtf?

Whatever happened to using fields\_for?

fields\_for @a.b do |b|  
b.text\_field “name”  
end

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 1:25am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/6 "2008-05-21T01:25:36Z")

</div>

> Oh my bad. You need to use
> 
> \<%= text\_field\_tag “b”, @a.b.name %\>
> 
> And then you’ll be able to access stuff with params[:b]

Excellent! This should get me going. Is there any way that I can  
have the tag formatted the way it is with the rest of the object? For  
example, in general if my class is Monster with various params I’d  
have

monster[height], monster[weight], etc with id’s being moster\_height,  
monster\_weight

And then of course this makes it really easy for me to just do  
@monster.update\_attributes(params[:monster])

Is there a way that I could have the tags for this set to be along  
those same lines, as follows:

monster[pet][type], monster[pet][name] with monster\_pet\_type,  
monster\_pet\_name

Either way, thanks a lot for the help!

-fREW

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 7:42am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/7 "2008-05-21T07:42:36Z")

</div>

> Well, that works really well, but what if I want that INSIDE of the  
> form\_for of @a? Then I get a nested which doesn’t make any  
> sense htmlicly speaking.

I said _fields\_for_ not form\_for.

It will work inside the form tag.

–  
Appreciated my help?  
Recommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 7:40am UTC](https://www.ruby-forum.com/t/form-for-usage/138195/8 "2008-05-21T07:40:51Z")

</div>

On May 20, 6:35 pm, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> wtf?
> 
> Whatever happened to using fields\_for?
> 
> fields\_for @a.b do |b|  
> b.text\_field “name”  
> end

Well, that works really well, but what if I want that INSIDE of the  
form\_for of @a? Then I get a nested which doesn’t make any  
sense htmlicly speaking.

-fREW

---

<div class="post-metadata">

**Author:** ![frew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c0e974/32.png) [@frew](https://www.ruby-forum.com/u/frew)\
**Post date:** [May 21, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/form-for-usage/138195/9 "2008-05-21T18:31:00Z")

</div>

Ah, sorry. Thanks much!

On May 21, 12:42 am, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
