# Mapping contained objects to forms?

**URL:** <https://www.ruby-forum.com/t/mapping-contained-objects-to-forms/57502>\
**Category:** Rails\
**Created:** [April 11, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/mapping-contained-objects-to-forms/57502 "2006-04-11T16:32:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 11, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/mapping-contained-objects-to-forms/57502/1 "2006-04-11T16:32:41Z")

</div>

I’m having lots of trouble trying to map an object that contains another  
object to the right forms param. So say I have Book which has one  
Publisher. Here is what I’ve tried:

Publisher  
\<%= text\_field 'book', 'publisher.name' %\>

Then

Publisher  
\<%= text\_field 'book', 'publisher[name]' %\>

And

Publisher  
\<%= text\_field 'book.publisher', 'name' %\>

All of them blow up with various exceptions. What is the right way to  
map a many to one relationship onto a form? And how will it know the ID  
of the contained object if it just has the name on the form? Is there  
something else I have to do to put the Publisher’s ID on the form so I  
can remember it? The Agile Web D. with Rails book is not  
helpful on this issue.

Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 12, 2006, 1:57am UTC](https://www.ruby-forum.com/t/mapping-contained-objects-to-forms/57502/2 "2006-04-12T01:57:32Z")

</div>

Bump. Any ideas? I’m sure someone knows how this works.

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [April 12, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/mapping-contained-objects-to-forms/57502/3 "2006-04-12T15:15:09Z")

</div>

On 4/11/06, Charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having lots of trouble trying to map an object that contains another  
> object to the right forms param. So say I have Book which has one  
> Publisher. Here is what I’ve tried:
> 
> Publisher  
> \<%= text\_field 'book', 'publisher.name' %\>

You cannot do the dotted method call with the standard Rails helpers  
AFAIK. I got frustrated with not being about to do this so wrote a  
helper for it (there are probably loads of better ways to achieve this  
but it got me going). My ‘custom\_text\_field’ generates a text field  
with a name of book[publisher][name] for your call.

I’m not entirely sure why this isn’t supported by Rails out-of-the-box.

Matt

class TagHelper  
def custom\_text\_field(object\_id, method, options = { })  
text\_field\_tag(  
object\_field\_name(object\_id, method),  
object\_value(object\_id, method),  
options  
)  
end

def object\_value(object\_id, method)  
method.split(“.”).inject(self.instance\_variable\_get(“@#{ object\_id  
}”)) { |o,m| o.nil? ? nil : o.send(m) }  
end

def object\_field\_name(object\_id, method)  
method.split(“.”).inject(object\_id) { |f,n| “#{ f }[#{ n }]” }  
end  
end
