1

I am trying to pass two local variables to my partial but I am getting this undefined local variable or method `row' for #<#:0x00000105d7f3b0> when using it in the partial.

Render

<%= f.fields_for :menu_items do |builder| %>
<%= render partial: 'menu_item_fields', locals: {f: builder, row: f.options[:child_index]} %>
<% end %>`

Partial

<a href="#" data-target="item-<%= f.options[:child_index].to_s + "-" + row.to_s %>" class="item-field"><%= f.object.title %></a>
6
  • Prpovide the line of code in which the error occouring? Commented Mar 17, 2014 at 16:36
  • I did. Its labeled "Partial" Commented Mar 17, 2014 at 16:37
  • I am using nested attributes and need a builder for the nested items Commented Mar 17, 2014 at 17:32
  • Sorry, I just had a "duh" moment. I meant to say since :menu_items is a collection and you are rendering a partial each time rendering a collection may be better suited for this as it executes once for every item in the collection. Commented Mar 17, 2014 at 17:36
  • I will look into that but I still am stuck with the same problem Commented Mar 17, 2014 at 17:48

2 Answers 2

1

Change this code

<a href="#" data-target="item-<%= f.options[:child_index].to_s + "-" + row.to_s %>" class="item-field"><%= f.object.title %></a>

as follows:

<a href="#" data-target="item-<%= f.options[:child_index].to_s + '-' + row.to_s %>" class="item-field"><%= f.object.title %></a>
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that and I have tried just doing this <%= row %> and it still gives the same error. Great catch though.
0

I think it should be :

{ f: builder, row: builder.options[:child_index] }

Oops, I misread your code on my mobile phone. Does your code work correctly if you don't call it using a partial?

4 Comments

If that was the case I would have no reason to pass row in the first place.
Right, what happens if you only pass f along? Like: parent_form: f. And then in your partial call parent_form.options[:child_index] ?
Same thing. It says undefined for the parent_form. The problem occurs when I have more than one local variable
Passing multiple values through locals should be no problem. You could test this by only passing parent_form and again call parent_form.options[:child_index]. It should still fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.