stringo0
stringo0

Reputation: 2760

Rails - Accessing Model Attributes in Forms

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 %>

Upvotes: 3

Views: 1444

Answers (2)

stringo0
stringo0

Reputation: 2760

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

Upvotes: 5

Jeff Paquette
Jeff Paquette

Reputation: 7127

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

Upvotes: 0

Related Questions