# How do you access attributes in form templates?

**URL:** <https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034>\
**Category:** Rails\
**Created:** [June 11, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034 "2010-06-11T20:16:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [June 11, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034/1 "2010-06-11T20:16:46Z")

</div>

I have a Survey-Response application, where surveys can be created  
with an arbitrary number of questions, and users can respond to an  
arbitrary number of surveys.

A Survey has\_many Questions and has\_many Responses. Both a Question  
and a Response has\_many Answers. That way, everybody has a  
relationship to everyone else.

In the Response#new for each Response.survey.questions I do a  
@response.answers.build and set the questions\_id attribute. That looks  
like this:  
1: survey = Survey.find(params[:survey\_id])  
2: @response = survey.responses.build  
3: @response.survey.questions.each do |question|  
4: @response.answers.build(:question\_id =\> question.id)  
5: end

All I want to do in the form template is get the Answer’s Question’s  
text to display as a label. That looks like this:

6: \<% f.fields\_for :answers do |fields| %\>  
7:

Q: \<%= Question.find(fields[:question\_id]).ask %\>

  
8: 

A: \<%= fields.text\_field :content %\>

  
9: \<% end %\>

Obviously Line 7 is junk, but that’s basically what I want to do. How  
do I get those prepopulated attribute values? I haven’t been able to  
find anything on this so it’s either obviously staring me in the face  
or an odd question.

I found this similar question, but it hasn’t received a reply.  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/115dbb1ba9215b0f/33779ed24e48c26c?lnk=gst&q=attributes+form#33779ed24e48c26c](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/115dbb1ba9215b0f/33779ed24e48c26c?lnk=gst&q=attributes+form#33779ed24e48c26c)

Thanks in advance.  
Dee

---

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034/2 "2010-06-17T16:22:15Z")

</div>

I haven’t had any responses in the past few days, so the problem must  
be unsolvable in Rails. 😉 LOL

But let me rephrase the question more succinctly.

Assume, Shelf has a description and a set of items. Item has a  
description, sku and color.  
Given, I use build in my controller’s new method to create a set of  
models like this:  
@shelf = Shelf.new  
3.times { @shelf.items.build(:sku =\> GenerateNewSku, :color =\>  
‘blue’) }

Explain how to display the sku and color in the form for each item?  
\<% form\_for @shelf do |f| %\>  
\<%= f.label :description %\>  
\<%= f.text\_field :description %\>

\<% f.fields\_for @shelf.items do |item\_fields| %\>  
\<%# **Want :sku here** #%\>  
\<%# **Want :color here** #%\>  
\<%= item\_fields.text\_field :description %\>  
\<% end %\>

\<%= f.submit %\>

  
\<% end %\>

Why I want this: My app has Surveys, and each has many Questions. Each  
Question has many Answers (one per user). The Answers also belong to a  
Response, which belong to a User. Response can be thought of as the  
user’s answers to a Survey. If that’s not clear, imagine a data  
diagram in the shape of a diamond, with Survey at the top branching  
down into Questions and Responses at the next level, and Answers at  
the bottom. Each level is has\_many with the one below it.

In the Response’s form, I want to display Question.ask for each  
question in the Survey as a label for each Answer in the Response.  
Maybe there is a better way to go about doing this, but I’ve tried  
numerous approaches, refactoring the databases, and am just about to  
switch over to .NET (jk).

---

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [June 17, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034/3 "2010-06-17T16:26:59Z")

</div>

Thanks for replying, Hassan.

I don’t think you’re missing the point, and, yes, that’s what _I_  
think it should be too. The problem is “item” is type  
ActionView:Helpers:FormBuilder, and therefore doesn’t have a sku  
property. Or maybe /I’m/ missing something

(I tried to make that clearer by naming it “item\_fields” rather than  
just “item” as you’ve done here.)

Dee

On Jun 15, 10:30 am, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034/4 "2010-06-17T16:22:26Z")

</div>

On Tue, Jun 15, 2010 at 7:14 AM, Dee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Explain how to display the sku and color in the form for each item?

> ```
> <% f.fields_for @shelf.items do |item_fields| %>
> <%# ****Want :sku here**** #%>
> 
> ```

Maybe I’m totally missing your point, but shouldn’t that just be, e.g.

```
  <% f.fields_for @shelf.items.each do |item| %>
            <%= item.sku %>
           ...

```

## ??

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [June 17, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034/5 "2010-06-17T16:26:59Z")

</div>

On Jun 15, 5:05 pm, Dee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for replying, Hassan.
> 
> I don’t think you’re missing the point, and, yes, that’s what _I_  
> think it should be too. The problem is “item” is type  
> ActionView:Helpers:FormBuilder, and therefore doesn’t have a sku  
> property. Or maybe /I’m/ missing something

if you’ve got a form builder then form\_builder.object is the object it  
is bound to.

Fred

---

<div class="post-metadata">

**Author:** ![dee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@dee](https://www.ruby-forum.com/u/dee)\
**Post date:** [June 17, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/how-do-you-access-attributes-in-form-templates/190034/6 "2010-06-17T16:34:43Z")

</div>

Thanks, that what I was looking for. Just found it in the source code.  
Works like a charm. Thanks Frederick.

On Jun 15, 12:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
