# Form with definitions and actuals - undefined method unless dummy field used

**URL:** <https://www.ruby-forum.com/t/form-with-definitions-and-actuals-undefined-method-unless-dummy-field-used/115667>\
**Category:** Rails\
**Created:** [September 27, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/form-with-definitions-and-actuals-undefined-method-unless-dummy-field-used/115667 "2007-09-27T13:16:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 27, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/form-with-definitions-and-actuals-undefined-method-unless-dummy-field-used/115667/1 "2007-09-27T13:16:14Z")

</div>

I’m trying to build a form, but haven’t quite got it sorted. Here’s  
the background:

There are form\_templates (e.g. questionnaires) and related questions.  
The form\_template model basically contains header information and acts  
as a parent to the questions. There is an arbitrary number of  
questions on a form\_template. We want to be able to administer a  
form\_template many times and change the questions over time, etc. So,  
there is a form\_actuals table that holds the header information for an  
instance of a “real” form\_template, and there is a form\_data table  
that holds the answers to the questions for a given form\_actual filled  
out by a person. A form\_template can have many form\_actuals; likewise  
a question can have many form\_data.

So,  
Definitions: form\_templates --\< questions  
Instances: form\_actuals --\< form\_data

To get the information into the database I want to be able to set up  
the definitions (form\_templates and questions) and then enter the  
actual responses (form\_actuals and form\_data).

The view (simplified):

```
 1 <% form_for :form_actual, :url => { :action => :create } do |

```

form| %\>  
2  
3

  
4 \<% @questions.each\_with\_index do |@question, index| %\>  
5 \<% fields\_for “form\_data[#{index}]”, @question do |f| %\>  
6 \<%= f.hidden\_field “question\_id”, :value =\>  
@question.id %\>  
7 \<%= f.hidden\_field “question\_text”, :value =\>  
@question.question %\>  
8  
9   
10   
11   
12   
13   
14  
15 \<% end %\>  
16 \<% end %\>  
17 

| \<%= @question.question\_number.to\_s %\> | \<%=h @question.question %\> | \<%= f.text\_field(“answer”) %\> |

  
18  
19 \<% end %\>

The form\_actuals controller:

def create  
@form\_actual = FormActual.new(params[:form\_actual])  
params[:form\_data].each\_value { |form\_data|  
@form\_actual.form\_data.build(form\_data) }

# If the ‘Save’ button is clicked.

if params[:save\_button] && @form\_actual.save  
etc…  
end

So, I start by iterating through the questions (line 4) and then  
creating fields for form\_data (line 5) based on them. I’m passing the  
id and text of a question to a form\_data record (lines 6 and 7) so  
that they are stored on a form\_data record (if a question is modified,  
it should not affect previously entered form\_data). Then I display the  
question number and text on the form (lines 10 and 11) and set up a  
field for the user to enter the answer (line 12).

The catch is this: “answer” is the column in form\_data that stores an  
actual response to a question, but I get an “undefined method `answer’  
for #Question:0x354feb8” error unless I add a dummy column “answer”  
to the questions table. If there is a column called “answer” in  
questions, then the form is created successfully and correctly (with  
indexed fields) and once it is submitted the controller does its magic  
to parse the form into separate records in form\_data.

This seems odd and I’m wondering if I have something fundamentally  
wrong here. Thanks for any insight, and apologies for the length of  
this post.
