Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Thanked 0 Times in 0 Posts

    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

    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

    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:

    class Document < ActiveRecord::Base
    has_many :options, :dependent => :destroy
    accepts_nested_attributes_for :options

    class Option < ActiveRecord::Base
    belongs_to :document

    I would really appreciate any help or suggestions.

  2. #2
    New Coder
    Join Date
    Apr 2010
    Thanked 3 Times in 3 Posts


    Need to known whether the problem is in storing or retrieving "options" for a document.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts