Formtastic and Access to Variables of Nested Model

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.

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

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

    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.

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.

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 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