I have attributes with special validation where I use the message clause to display a special message just for that validation. Here is one example:
validates :email, presence: true, length: { maximum: 60 },
format: { with: valid_email_regex, message: "is not a valid email address format." },
uniqueness: { case_sensitive: false }
I would like to translate the message here but I am not sure how to do it.
I have seen examples where they type something like this: message: t("some_value_here"). I'm not sure about the designation. I tried something like this message: t(:bad_email). I did the following in my yaml file just to try something.
activemodel:
errors:
bad_email: "is not a valid email address format."
When I tried to access my Rails application I got the following error:
ActionView::Template::Error (undefined method `t' for #<Class:0x007fefc1b709e0>)
I also tried this in my yaml file:
activemodel:
errors:
user:
bad_email: "is not a valid email address format."
I have been researching this off and on all day long. All I can find is to replace built-in error hashes like blank or empty. Is there a way for me to have custom error hashes and replace them in the model? At this point I cannot get the t to work as coded. My hope is that the problem is how I have my yaml file set up. I have seen varying versions of how to set this up. I am not sure if I should put this under activemodel or activerecord. I assumed activemodel since that is where the custom message is that I want to translate.
Any help would be appreciated. This is the last piece I need to figure out before launching my first translation of the application.
UPDATE 7/29/2013 7:30 pm CDT
bgates gave me a very good start with how to setup my model files to receive the custom message in the YAML file. However I ended up having to do the following setup in my yaml file for the custom messages to be found.
activerecord:
errors:
models:
user:
attributes:
bio:
no_links: "cannot contain email addresses or website links (URLs)."
email:
bad_email: "is not a valid email address format."
username:
bad_username: "can only contain numbers and letters. No special characters or spaces."
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…