I’m hoping this is a simple question – I have the following helper code:
module ApplicationHelper
def add_feature_fields(feature_types, object_form_builder, actions_visible)
feature_types.length.times {object_form_builder.object.features.build}
i = 0
fields = object_form_builder.fields_for :features do |features_builder|
render :partial => "features/fixed_feature", :locals => {:feature => features_builder, :fixed_feature_type => feature_types[i], :form_actions_visible => actions_visible}
i = i + 1
end
end
end
The code is working as expected, except for the line i = i + 1. For some reason, this seems to be breaking the loop, and nothing is rendered. Evidently, I am doing this wrong somehow – perhaps fields_for is not a normal loop?
How can I increment i by 1 each time the loop runs?
I was able to get this working by doing the following:
I believe what was happening was that when I did
i = i + 1after I called render, the return value was the iterator and not render (since the method returns the last value).