开发者

change the name of returning field with errors in rails

开发者 https://www.devze.com 2022-12-13 17:46 出处:网络
I have a form like this : <fieldset> <legend> اضافه کردن یک قالب </legend>

I have a form like this :

<fieldset>
    <legend>
        اضافه کردن یک قالب
    </legend>
    <%= error_messages_for 'theme' , :header_message =开发者_如何学编程> "موارد زیر دارای اشکال می باشند، لطفا دوباره بررسی نمایید :" , :message => nil %>
    <ol>
        <% form_for @theme do |t| %>
        <li>
            <%= label :theme , :نام %>
            <%= t.text_field :name %>
        </li>
        <li>
            <%= label :theme , :نام_انگلیسی %>
            <%= t.text_field :en_name %>
        </li>
        <li>
            <%= label :theme , :قیمت %>
            <%= t.text_field :price %>
        </li>
        <li>
            <%= label :theme , :قیمت_ویژه %>
            <%= t.text_field :unique_price %>
        </li>
        <li>
            <%= label :theme , :توضیحات %>
            <%= t.text_area :description %>
        </li>
        <li>
            <%= submit_tag "اضافه کردن قالب" %>
        </li>
        <% end %>
    </ol>
</fieldset>

in my model i have a validation :

  validates_presence_of :name , :en_name , :price , :unique_price , :description , :message => "نباید خالی باشد"

the problem is i want to have a unicode(persian) name in my validation error message, currently it returns something like this error :

موارد زیر دارای اشکال می باشند، لطفا دوباره بررسی نمایید :

    * Name نباید خالی باشد

all i want to do is somehow change the 'Name' in error message to a persian word, what can I do ?


Since 2.3, the proper way to do it is to use i18n. See: http://guides.rubyonrails.org/i18n.html section 5.2.1.


You need to have a look at humanized attributes

in your model (each model)

  HUMANIZED_ATTRIBUTES = {
    :name => "نا"
  }

  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end
0

精彩评论

暂无评论...
验证码 换一张
取 消