I have a form and I want to show some form elements x times dependent on languages in Langs table, it is nested attributes with has_many association. I am trying to use fields_for but with no success.
I have a Lang objects and this code in template.
<%= fields_for :categories_versions, @langs do |lang| %>
<div class="card mt-4 mb-4">
<div class="card-body">
<div class="container mt-4">
<%= "Category for #{Lang.find(lang.id).lang}" %><br/>
it yields ActionView object, but I want to access every lang in @langs (which is simply Lang.all.
And it generates me just one set, but I have 2 languages, so my desired output is form with 2 sets of columns for every of two languages with name of the language.
What am I doing wrong?
The object that is given to you by
fields_for(namely the|lang|in your example) is a form helper, not an object from the@langcollection. So to get the id of currentlangyou would need to dolang.object.id. I'd suggest you change|lang|to|lang_f|to avoid confusion. Also if@langsis not of the same type as:categories_versions, I am not sure this is going to work properly. You probably want something like:I am making some assumptions here, so adjust to your actual attributes and models.