Undefined local variable or method `__nested_field_for_replace_with_index__'
I am trying to set multiple placeholders, one for each form field, from an array of placeholders. It sounds like the __nested_field_for_replace_with_index__ string would be useful, but I'm getting the following error:
undefined local variable or method `__nested_field_for_replace_with_index__' for #<#<Class:0x007f7fa3821188>:0x007f7f9ef8a578>
Here's my code:
/ _form.html.haml
= vertical_form_for study, url: study_questions_path(study.slug), method: :post do |f|
= f.nested_fields_for :questions do |question|
= render partial: 'form_fields', locals: { f: question, placeholder: Question::PLACEHOLDERS[__nested_field_for_replace_with_index__] }
/_form_fields.html.haml
= f.input :body, placeholder: placeholder, label: 'Open-ended question', as: :text, input_html: { class: 'question-textarea' }
I also tried just putting it there as a string, but then I get no implicit conversion of String into Integer as the error instead.
Try something like this:
= vertical_form_for study, url: study_questions_path(study.slug), method: :post do |f|
= f.nested_fields_for :questions do |question|
- index = question.object ? study.questions.index(question.object) : '__nested_field_for_replace_with_index__'
= render partial: 'form_fields', locals: { f: question, placeholder: Question::PLACEHOLDERS[index] }
And make sure that study.questions are ordered somehow.
I did something like this quite a while ago and I haven't tested this. But it should work I think.
Basically it uses teh correct index for the existing study.questions and won't give you that no implicit conversion of String into Integer error. But for the form template which is hidden and handled with js when adding fields, the placeholder is used and the correct index is replaces it when adding nested fields.