레일즈 에러를 한글화

armyinsa의 이미지

<%= error_messages_for 'inquiry' %>를 이용하여 메세지를 출력하는데요..~~!!

아래와같이

7 errors prohibited this inquiry from being saved

There were problems with the following fields:

* Name 을 입력해주세요。
* Subject 을 입력해주세요。
* Tel num 을 입력해주세요。
* Tel num 은 숫자를 입력해주세요。
* Content 을입력해주세요。
* Email 을 입력해해주세요。
* Email 형식이 맞지 않습니다。

에러가 나옵니다.

7 errors prohibited this inquiry from being saved

There were problems with the following fields:
부분을 한글로 만들고 싶은데요...

혹시 해결하신분요~~!>>

armyinsa의 이미지


* Name 을 입력해주세요。
* Subject 을 입력해주세요。
* Tel num 을 입력해주세요。
* Tel num 은 숫자를 입력해주세요。
* Content 을입력해주세요。
* Email 을 입력해해주세요。
* Email 형식이 맞지 않습니다。

name 이름
Subject 제목
한글화 하는 작업은 아래와 같으 작업으로 한글화가 됩니다.

class Kids < ActiveRecord::Base
class << self
HUMANIZED_ATTRIBUTE_KEY_NAMES = {
"name" => "이름",
"Subject " => "제목",
}

def human_attribute_name(attribute_key_name)
HUMANIZED_ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
end
end

이렇게 하면되구요~~!! 혹시 나같이 고생안하기 위해.~~!!

두번째 문제는
7 errors prohibited this inquiry from being saved

There were problems with the following fields:

인데...

어떻게 해결을 해야할지... 찾아보고 있습니다.~~~!! 나 마징가~~!

armyinsa의 이미지

7 errors prohibited this inquiry from being saved

There were problems with the following fields

대한 문제 방금해결 했습니다.

7개의 에러가 발생했습니다.

다음의 항목에 문제가 있습니다.

이름 을 입력해주세요。 제목 을 입력해주세요。 전화번호 을 입력해주세요。 전화번호 은 숫자를 입력해주세요。 내용 을입력해주세요。 이메일 을 입력해해주세요。 이메일 형식이 맞지 않습니다。

이런식으로 방법은요

# app/helpers/application_helper.rb
def template_error_messages_for (object_name, options = {})
options = options.symbolize_keys
object = instance_variable_get("@#{object_name}")
unless object.errors.empty?
render :partial=>"system/error_messages_for",
:locals=>{:messages=>object.errors.full_messages, :object=>object}
end
end

# app/views/system/_error_messages_for.rhtml


<%= messages.size %>개 에러가 발생했습니다.


다음항목에 문제가 있습니다.



    <% for mes in messages %>
  • <%= mes %>
  • <% end %>

이렇게 하면

한글화 작업 전부 다 해결됩니다.

armyinsa의 이미지

# app/helpers/application_helper.rb
def template_error_messages_for (object_name, options = {})
options = options.symbolize_keys
object = instance_variable_get("@#{object_name}")
unless object.errors.empty?
render :partial=>"system/error_messages_for",
:locals=>{:messages=>object.errors.full_messages, :object=>object}
end
end

# app/views/system/_error_messages_for.rhtml

<"errorExplanation" id="errorExplanation">

<%= messages.size %>개 에러가 있습니다.

다음항목

<% for mes in messages %>

  • <%= mes %>
  • <% end %>
    armyinsa의 이미지


    이상하게 나오네요..미안해용~~!!