From e4e1e338b2eb3e09729a2c003b93109c5d799564 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Thu, 26 Feb 2015 15:17:25 +0000 Subject: [PATCH] Build translations before attempting to save --- ...admin_public_body_categories_controller.rb | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/app/controllers/admin_public_body_categories_controller.rb b/app/controllers/admin_public_body_categories_controller.rb index e203f20cfb4..f6c3b0b2d8e 100644 --- a/app/controllers/admin_public_body_categories_controller.rb +++ b/app/controllers/admin_public_body_categories_controller.rb @@ -83,6 +83,11 @@ def update def create I18n.with_locale(I18n.default_locale) do @category = PublicBodyCategory.new(params[:public_body_category]) + # Build a translation for the default locale as it isn't available + # until the parent record is persisted + @category.translations.build(:locale => I18n.default_locale, + :title => params[:public_body_category][:title], + :description => params[:public_body_category][:description]) if @category.save # FIXME: This can't handle failure (e.g. if a PublicBodyHeading @@ -95,20 +100,6 @@ def create flash[:notice] = 'Category was successfully created.' redirect_to admin_categories_path else - I18n.available_locales.each do |locale| - if locale == I18n.default_locale - @category.translations.build(:locale => locale) - next - end - - translation_params = params[:public_body_category]. - fetch(:translations_attributes, {}). - fetch(locale, nil) - - if !@category.translations.where(:locale => locale).first && translation_params - @category.translations.build(translation_params) - end - end render :action => 'new' end end