diff --git a/backend/Gemfile b/backend/Gemfile index 2d0a90c..7b44641 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -69,3 +69,5 @@ gem 'discard' gem "rspec-rails", "~> 8.0", :groups => [:development, :test] gem 'aws-sdk-s3', require: false + +gem 'rails-i18n', '~> 8.0.0' diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index f9dc02c..555f867 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -306,6 +306,9 @@ GEM rails-html-sanitizer (1.6.2) loofah (~> 2.21) nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) + rails-i18n (8.0.2) + i18n (>= 0.7, < 2) + railties (>= 8.0.0, < 9) railties (8.0.2) actionpack (= 8.0.2) activesupport (= 8.0.2) @@ -477,6 +480,7 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 8.0.2) + rails-i18n (~> 8.0.0) rspec-rails (~> 8.0) rubocop-rails-omakase sprockets-rails diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index 976578d..7bddd07 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -453,7 +453,7 @@ class PostsController < ApplicationController if missing_ids.present? post.errors.add :parent_post_ids, - "存在しない親投稿 ID があります: #{ missing_ids.join(' ') }" + "存在しない親投稿 Id. があります: #{ missing_ids.join(' ') }" raise ActiveRecord::RecordInvalid, post end diff --git a/frontend/src/lib/apiErrors.test.ts b/frontend/src/lib/apiErrors.test.ts index 32d6250..fbbc67a 100644 --- a/frontend/src/lib/apiErrors.test.ts +++ b/frontend/src/lib/apiErrors.test.ts @@ -46,7 +46,7 @@ describe ('extractValidationError', () => { }) expect (validationError?.fieldErrors).toEqual ({ - 'deerjikists.0.platform': ['プラットフォームを入力してください.'], + 'deerjikists0Platform': ['プラットフォームを入力してください.'], }) }) diff --git a/frontend/src/lib/apiErrors.ts b/frontend/src/lib/apiErrors.ts index 72496d1..9a1181f 100644 --- a/frontend/src/lib/apiErrors.ts +++ b/frontend/src/lib/apiErrors.ts @@ -1,3 +1,5 @@ +import toCamel from 'camelcase-keys' + import { isApiError } from '@/lib/api' export type FieldErrors = Partial> @@ -17,13 +19,13 @@ export const extractValidationError = (err: unknown) if (!(isApiError (err)) || err.response?.status !== 422) return null - const rawData = (err.response.data ?? { }) as Record + const rawData = toCamel ((err.response.data ?? { }) as Record, + { deep: true }) as RawValidationError const data: RawValidationError = { - type: rawData.type as string | undefined, - message: rawData.message as string | undefined, - errors: rawData.errors as Record | undefined, - baseErrors: rawData.base_errors as string[] | undefined, - } + type: rawData.type as string | undefined, + message: rawData.message as string | undefined, + errors: rawData.errors as Record | undefined, + baseErrors: rawData.baseErrors as string[] | undefined } if (data.type !== 'validation_error' && !(data.errors)) return null diff --git a/frontend/src/pages/deerjikists/DeerjikistDetailPage.tsx b/frontend/src/pages/deerjikists/DeerjikistDetailPage.tsx index a6a3647..99dd7ad 100644 --- a/frontend/src/pages/deerjikists/DeerjikistDetailPage.tsx +++ b/frontend/src/pages/deerjikists/DeerjikistDetailPage.tsx @@ -20,7 +20,7 @@ import type { FC, FormEvent } from 'react' import type { Deerjikist, Platform } from '@/types' type DeerjikistFormField = - 'deerjikists' | `deerjikists.${ number }.platform` | `deerjikists.${ number }.code` + 'deerjikists' | `deerjikists${ number }Platform` | `deerjikists${ number }Code` const DeerjikistDetailPage: FC = () => { @@ -105,45 +105,45 @@ const DeerjikistDetailPage: FC = () => { {/* プラットフォーム */} + messages={fieldErrors[`deerjikists${ i }Platform`]}> {({ describedBy, invalid }) => ( - )} + )} {/* コード */} + messages={fieldErrors[`deerjikists${ i }Code`]}> {({ describedBy, invalid }) => ( - setData (prev => { - const rtn = [...prev] - rtn[i] = { ...rtn[i], code: e.target.value } - return rtn - })}/>)} + setData (prev => { + const rtn = [...prev] + rtn[i] = { ...rtn[i], code: e.target.value } + return rtn + })}/>)} ))}