rilla / batch_translations

Helpers to allow saving multiple Globalize2 translations in the same request
http://rilla.es
14 stars 27 forks source link

multiple globalize_fields_for per same locale #2

Open piccio opened 12 years ago

piccio commented 12 years ago

I'm using globalize3 and batch_translations

I have a form with multiple globalize_fields_for per same locale like this one:

<%= form_for(@article) do |f| %>
    <%= f.globalize_fields_for :en do |g| %>
      <%= g.text_field :title %>
    <% end %>

    <%= f.globalize_fields_for :it do |g| %>
      <%= g.text_field :title %>
    <% end %>

    <%= f.globalize_fields_for :en do |g| %>
      <%= g.text_field :body %>
    <% end %>

    <%= f.globalize_fields_for :it do |g| %>
      <%= g.text_field :body %>
    <% end %>
<% end %>

when submitting the form to create a new article the paramaters params[:article][:translated_attributes] that are posted are like this:

{"1"=>{"title"=>"title it", "id"=>"", "locale"=>"it"}, "2"=>{"title"=>"title en", "id"=>"", "locale"=>"en"}, "3"=>{"body"=>"body it", "id"=>"", "locale"=>"it"}, "4"=>{"body"=>"body en", "id"=>"", "locale"=>"en"}}

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:

{"it"=>{"title"=>"title it", "id"=>"", "locale"=>"it", "body"=>"body it"}, "en"=>{"title"=>"title en", "id"=>"", "locale"=>"en", "body"=>"body en"}}

and the article object store translations correctly

this not happening when submitting edit form because translation id is posted

jrom commented 11 years ago

Thanks @piccio!