this cause the model article store four translations, two for :it and two for :en, instead of one :it and one :en
+----+------------+---------+------------+-----------+
| id | article_id | locale | title | body |
+----+------------+---------+------------+-----------+
| 49 | 25 | it | value it | |
| 50 | 25 | en | value en | |
| 51 | 25 | it | | body it |
| 52 | 25 | en | | body en |
+----+------------+---------+------------+-----------+
I have fixed it by overriding globalize_fields_for method of batch_translations gem:
module ActionView
module Helpers
class FormBuilder
def globalize_fields_for(locale, *args, &proc)
raise ArgumentError, "Missing block" unless block_given?
@index = locale
object_name = "#{@object_name}[translations_attributes][#{@index}]"
object = @object.translation_for(locale)
@template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
@template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
if @template.respond_to? :simple_fields_for
@template.simple_fields_for(object_name, object, *args, &proc)
else
@template.fields_for(object_name, object, *args, &proc)
end
end
end
end
end
in particular I have fixed the original row:
@index = @index ? @index + 1 : 1
with:
@index = locale
now the paramaters params[:article][:translated_attributes] that are posted are like this:
I'm using globalize3 and batch_translations
I have a form with multiple globalize_fields_for per same locale like this one:
when submitting the form to create a new article the paramaters params[:article][:translated_attributes] that are posted are like this:
this cause the model article store four translations, two for :it and two for :en, instead of one :it and one :en
I have fixed it by overriding globalize_fields_for method of batch_translations gem:
in particular I have fixed the original row:
with:
now the paramaters params[:article][:translated_attributes] that are posted are like this:
and the article object store translations correctly
this not happening when submitting edit form because translation id is posted