rilla / batch_translations Goto Github PK
View Code? Open in Web Editor NEWHelpers to allow saving multiple Globalize2 translations in the same request
Home Page: http://rilla.es
Helpers to allow saving multiple Globalize2 translations in the same request
Home Page: http://rilla.es
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)
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.