Go Back   CodingForums.com > :: Server side development > Ruby & Ruby On Rails

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2010, 03:16 AM   PM User | #1
anmueller
New to the CF scene

 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
anmueller is an unknown quantity at this point
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.
anmueller is offline   Reply With Quote
Old 04-01-2010, 01:47 PM   PM User | #2
katierosy
New Coder

 
Join Date: Apr 2010
Posts: 38
Thanks: 0
Thanked 3 Times in 3 Posts
katierosy is an unknown quantity at this point
Smile

Need to known whether the problem is in storing or retrieving "options" for a document.
katierosy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:20 AM.


Advertisement
Log in to turn off these ads.