Git Product home page Git Product logo

batch_translations's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

batch_translations's Issues

empty input fields after validation error

I'm using globalize3 and batch_translations

when submitting a form and there are model validation's errors the form that is rendered has input fields empty only on translated attributes.

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 = @index ? @index + 1 : 1
        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

object = @object.globalize_translations.find_by_locale locale.to_s

with

object = @object.translation_for(locale)

multiple globalize_fields_for per same locale

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.