Flaviu Simihaian's Blog - Entrepreneur and Developer

Get the Model in a Fields_for Block

I have a fields_for block in my view, like this:

1
= form_survey.fields_for :questions, (@survey.questions) do  |form_questions|

But I want to access each question model directly within the block. I tried question or @question and after endless searches, I found the object attribute:

1
form_questions.object

which contains all this awesome:

1
2
3
4
5
6
7
8
9
--- !ruby/ActiveRecord:Question
    attributes:
      id: "65"
      survey_id: "42"
      text: "0"
      created_at: 2011-11-03 18:24:02.746409
      updated_at: 2011-11-03 18:24:02.746409
      position: "0"
      question_type: SINGLE

Yes! I hope this saves another Flaviu some time.

Comments

Fork Flaviu on GitHub