# Formtastic and Access to Variables of Nested Model

**URL:** <https://www.ruby-forum.com/t/formtastic-and-access-to-variables-of-nested-model/181692>\
**Category:** Rails\
**Created:** [January 16, 2010, 3:54am UTC](https://www.ruby-forum.com/t/formtastic-and-access-to-variables-of-nested-model/181692 "2010-01-16T03:54:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anmueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90ced4/32.png) [@anmueller](https://www.ruby-forum.com/u/anmueller)\
**Post date:** [January 16, 2010, 3:54am UTC](https://www.ruby-forum.com/t/formtastic-and-access-to-variables-of-nested-model/181692/1 "2010-01-16T03:54:47Z")

</div>

So I’m not even sure if I’ll be able to properly explain what I’m  
trying to do, but I’ll do my best.

Let’s say that I have two models: Document and Option.

A Document has many Options (and, by extension, an Option belongs to a  
Document).

Each document may have a different number of options, hence why I made  
it a separate model.

Each document has the following fields: “name” and “content”.

Each option has the following fields (aside from the obvious  
“document\_id”): “name”, “hint”, and “value”.

I would like to create a form for a document, with nested fields for  
that document’s options. The tricky part is that I would like to use  
the fields listed above in my builder, for instance for the hint or  
label.

I’m sorry if I’m not making myself very clear. I’ve tried to put  
together an example with pseudo code for what I would like to do. I’ve  
put what I want to have appear as follows **expected value**.

\<% semantic\_form\_for @document do |f| %\>  
\<% f.inputs do %\>  
\<%= f.input :name %\>  
\<%= f.input :content %\>  
\<% end %\>

\<% f.inputs :name =\> ‘Option #%i:’, :for =\> :options do |c| %\>  
\<%= c.input :value, :label =\> **option “name” field** , :hint =\>  
**option “hint” field** %\>  
\<% end %\>

\<% f.buttons do %\>  
\<%= f.commit\_button ‘Submit’ %\>  
\<% end %\>

\<% end %\>

For example, let’s say my document has the following three options:  
Option 1:  
name = “color”  
hint = “Please provide the color name.”  
value = “”

Option 2:  
name = “brand”  
hint = “Please provide the brand name.”  
value = “”

Option 3:  
name = “age”  
hint = “How old are you?”  
value = “”

I’d like the form to provide the label for each of the three options  
using the “name” field, and then use the “hint” field to provide the  
form input hint. If I understand things correctly, at the very least,  
my example should already store the user input for each option in the  
“value” field.

Here are my models in case you need them:

Document.rb:  
class Document \< ActiveRecord::Base  
has\_many :options, :dependent =\> :destroy  
accepts\_nested\_attributes\_for :options  
end

Option.rb:  
class Option \< ActiveRecord::Base  
belongs\_to :document  
end

I would really appreciate any help or suggestions.

---

<div class="post-metadata">

**Author:** ![anmueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90ced4/32.png) [@anmueller](https://www.ruby-forum.com/u/anmueller)\
**Post date:** [January 17, 2010, 2:02am UTC](https://www.ruby-forum.com/t/formtastic-and-access-to-variables-of-nested-model/181692/2 "2010-01-17T02:02:15Z")

</div>

Strangely, this code…

\<% f.semantic\_fields\_for :options do |c| %\>  
\<% @document.options.each do |option| %\>  
\<%= c.input :value, :label =\> option.name, :hint =\> option.hint  
%\>  
\<% end %\>  
\<% end %\>

… end’s up posting the names, the form fields and the hints twice.  
Also it seems that they are now somehow outside of Formtastic’s reach  
as it is showing bullet-points.

See: [http://ils.unc.edu/~muela/first.png](http://ils.unc.edu/~muela/first.png)

Switching it around, like so…

\<% @document.options.each do |option| %\>  
\<% f.semantic\_fields\_for :options, option do |c| %\>  
\<%= c.input :value, :label =\> option.name, :hint =\> option.hint  
%\>  
\<% end %\>  
\<% end %\>

…shows it only once, but still not within the Formtastic

's. So  
each item still gets shown as a - with its own bullet point. Also  
it’s not getting the css through Formtastic like a normal  
semantic\_fields\_for setup would.

See:: [http://ils.unc.edu/~muela/second.png](http://ils.unc.edu/~muela/second.png)

This is getting somewhere though. Wonder if there’s some way to get  
that last bit fixed.

Any help would be greatly appreciate it. I really figured that I would  
just have been able to do something like “:label =\> :name” or “:label  
=\> option.name” without having to resort to the  
“@document.options.each do” bit, which I think is breaking the  
Formtastic magic.

Please help.

---

<div class="post-metadata">

**Author:** ![anmueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90ced4/32.png) [@anmueller](https://www.ruby-forum.com/u/anmueller)\
**Post date:** [January 18, 2010, 12:07am UTC](https://www.ruby-forum.com/t/formtastic-and-access-to-variables-of-nested-model/181692/3 "2010-01-18T00:07:39Z")

</div>

Alexis Mueller wrote:

> …  
> I would really appreciate any help or suggestions.

I don’t know Formtastic and can’t find docs for it. For standard nested  
forms there is an ‘object’ method:

\<% f.fields\_for :options do | of | %\>  
\<%= of.text\_field :value, :label =\> of.object.name, :hint =\>  
of.object.hint %\>  
\<% end %\>

There should be some equivalent (I would even expect it to have the same  
name).

Hope this helps, T.

---

<div class="post-metadata">

**Author:** ![anmueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90ced4/32.png) [@anmueller](https://www.ruby-forum.com/u/anmueller)\
**Post date:** [January 20, 2010, 8:35am UTC](https://www.ruby-forum.com/t/formtastic-and-access-to-variables-of-nested-model/181692/4 "2010-01-20T08:35:13Z")

</div>

T. N. T. wrote:

> Alexis Mueller wrote:
> 
> > …  
> > I would really appreciate any help or suggestions.
> 
> I don’t know Formtastic and can’t find docs for it. For standard nested  
> forms there is an ‘object’ method:
> 
> \<% f.fields\_for :options do | of | %\>  
> \<%= of.text\_field :value, :label =\> of.object.name, :hint =\>  
> of.object.hint %\>  
> \<% end %\>
> 
> There should be some equivalent (I would even expect it to have the same  
> name).
> 
> Hope this helps, T.

Hi T.

I had posted the same question on [railsforum.com](http://railsforum.com) and someone came up  
with the same response. If I weren’t so impatient I’d read the entire  
API doc for Rails so that I can know about a few more of these tricks.

In any event, thanks for your help.

Sincerely,  
Alexis
