How to save multiple translation from 1 object without to reset I18n.locale with Globalize?

StackOverflow https://stackoverflow.com/questions/22495079

  •  17-06-2023
  •  | 
  •  

سؤال

I'm trying to set my controller to save multiple languages with an object like this:

{ text: { fr: "francais", en: "English" } }
هل كانت مفيدة؟

المحلول

In your controller ex: (models_controller.rb)

def create
  model = Model.new model_save_param
  model.save
end
def model_save_params
  translations_attributes: params[:model][:text].map { |locale, translation| {locale: locale, text: translation} }
end

In your model ex: (model.rb)

translates :text
accepts_nested_attributes_for :translations

Or, there is a Gem Globalize Accessor that can do the job

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top