views:

15

answers:

2

Hi,

How do I access a model's parent's attribute in a form? For example, for the following form for answer, I want to access answer.question.text and use that for the question - how do I do this?

Thanks!

<% form_for :answers do |ans| %>
    <%= ans.label :question, "Question" %>
    <%= ans.text_field :value %>
A: 

Did you try ans.question.text ? You may need to ensure question has been loaded already via an :include => question in your answer finder.

Jeff Paquette
I get an undefined method 'question' error when simply trying ans.question.text. I'm not sure how to ensure question is loaded - I tried f.fields_for :answers, :include => question do, but I get the same error.
stringo0
+1  A: 

I ended up using ans.object.question.text - didn't know you could do form.object! wow!

stringo0