Rails 4 и globalize без добавления перевода

Я обновился до Rails 4 и globalize (вместо globalize3), и с тех пор переводы работают неправильно.

Шаги:

создать английскую запись
изменить локаль на :es
затем вызвать update_attributes для только что созданного объекта

Это использовалось для создания нового перевода в es.
Но вместо этого он модифицирует английскую запись!!! Пожалуйста, помогите?

Gemfile:

gem "rails", "4.0.1"
gem "globalize", "~> 4.0.0.alpha.1"

Модель:

class GlossaryTerm < ActiveRecord::Base
 translates :term, :definition
 accepts_nested_attributes_for :translations

 has_many :glossary_term_translations
 belongs_to :section
 validates_presence_of :term
 validates_presence_of :definition

 **after_create :create_spanish_placeholder**

 def create_spanish_placeholder
   term = self.term
   definition = self.definition
   I18n.locale = :es
   self.update_attributes(:term => "SPANISH placeholder for #{term}", :definition => "SPANISH placeholder for #{definition}")
   I18n.locale = :en
 end

#...

end

Контроллер:

class Admin::GlossaryTermsController < ApplicationController
before_filter :authorize_sysadmin!

def create
  find_models
  @glossary_term = @section.glossary_terms.new(glossary_term_params)
  if @glossary_term.save
    redirect_to edit_admin_section_url(@section, program_id: @program.id)
  else
    render :action => "new"
  end
end

#...

 protected

 def glossary_term_params
   params.require(:glossary_term).permit(:term, :definition, :glossary_image,   :glossary_image_file_name,
                                    :translations_attributes => [:id, :term, :definition])
 end

 #...

 end

person user3088213    schedule 10.12.2013    source источник
comment
Вы уже пробовали gem 'globalize', '~> 4.0.0.alpha.2'?   -  person wintermeyer    schedule 11.12.2013
comment
вы нашли решение этого? Если да, то не могли бы вы ответить на свой вопрос?   -  person Pavan Katepalli    schedule 19.08.2014


Ответы (1)


Попробуйте этот метод:

def create_spanish_placeholder
  term = self.term
  definition = self.definition
  Globalize.with_locale(:es) do
    self.update_attributes(:term => "SPANISH placeholder for #{term}", :definition => "SPANISH placeholder for #{definition}")
  end
end
person retro    schedule 07.12.2014