Rails Error Messages Without Attribute Name
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this rails validation change attribute name site About Us Learn more about Stack Overflow the company Business Learn more
Rails Custom Validation Error Message
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Rails Change Field Name Error Message
Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error messages always
Rails Validation Field Name
include attribute name up vote 7 down vote favorite 6 I have the following validation error messages coming up when I try to submit a blank form: Start time time Looks like you forgot the appointment start time. Start time time Sorry, we can't understand "" as a time. Start time ymd Please choose a date for the appointment. Start time ymd Sorry, we rails human_attribute_name can't understand "" as a date. Stylist services Please choose at least one service. These messages are for the following attributes: start_time_time start_time_time start_time_ymd start_time_ymd stylist_services I included the attribute names so you could plainly see which part of the error message is the attribute name. How do I remove the attribute names from the error messages? ruby-on-rails ruby-on-rails-3 share|improve this question asked Apr 3 '11 at 0:25 Jason Swett 14.4k33120225 What code are you using in your view to display the error messages? –Michelle Tilley Apr 3 '11 at 0:57 You pretty much guessed it in your answer, Brandon. What you put there is helpful and good to know. The other answer happens to be a better solution in my case but I wish I could select both answers as correct. –Jason Swett Apr 3 '11 at 1:27 add a comment| 4 Answers 4 active oldest votes up vote 13 down vote accepted You could use the i18n route to change the display name of the attribute. config/locales/en.yml: en: activerecord: attributes: somemodel: start_time_time: My Start Time Text #renamed text stylist_services: "" #hidden txet share|improve this answer answ
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of rails errors full messages this site About Us Learn more about Stack Overflow the company Business Learn rails error message without field name more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question i18n ruby x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up http://stackoverflow.com/questions/5526762/error-messages-always-include-attribute-name Rails validation error not including attribute name up vote 0 down vote favorite I know I can fix this issue with i18n, which I will try to do if I have the time, but for a quicker fix I was curious: Is there a way to add the attribute name to a rails validation error? For example, I have the following in my http://stackoverflow.com/questions/12684486/rails-validation-error-not-including-attribute-name model: validates_presence_of :name The validation error rendered is can't be blank. I'd prefer it to say Name can't be blank. ruby-on-rails validation share|improve this question asked Oct 2 '12 at 4:19 tvalent2 2,05973169 add a comment| 3 Answers 3 active oldest votes up vote 0 down vote accepted Use errors.full_messages:
- <% @record.errors.full_messages.each do |msg| %>
- <%= msg %> <% end %> share|improve this answer answered Oct 2 '12 at 4:32 bricker 7,43613249 add a comment| up vote 1 down vote validates_presence_of :name, :format => { :message => "whatever you want" } share|improve this answer answered Oct 2 '12 at 4:21 Ionut Hulub 3,55031137 1 That simply will not work. You're mixing up the class-method-style validations with the key-based validations. –bricker Oct 2 '12 at 4:34 add a comment| up vote 0 down vote Only - validates_presence_of :name, :message => "Name can't be blank" for validates_presence_of , error-locale is - "can't be blank"; attribute/property name is prefixed during validation. You can override this by above. So, for your case, since property name is 'Name', error will automatically become "Name can't be blank". To override the
helpers. A minimal implementation could be: class Person # Required dependency for ActiveModel::Errors extend ActiveModel::Naming def initialize @errors http://api.rubyonrails.org/classes/ActiveModel/Errors.html = ActiveModel::Errors.new(self) end attr_accessor :name attr_reader :errors def validate! http://adamhooper.com/eng/articles/5-full-sentences-in-rails-validation-messages errors.add(:name, :blank, message: "cannot be nil") if name.nil? end # The following methods are needed to be minimally implemented def read_attribute_for_validation(attr) send(attr) end def self.human_attribute_name(attr, options = {}) attr end def self.lookup_ancestors [self] end end The last three methods error message are required in your object for Errors to be able to generate error messages correctly and also handle multiple languages. Of course, if you extend your object with ActiveModel::Translation you will not need to implement the last two. Likewise, using ActiveModel::Validations will handle the validation related methods for you. rails error message The above allows you to do: person = Person.new person.validate! # => ["cannot be nil"] person.errors.full_messages # => ["name cannot be nil"] # etc.. Methods # [], []= A add, add_on_blank, add_on_empty, added?, as_json B blank? C clear, count D delete E each, empty? F full_message, full_messages, full_messages_for G generate_message, get H has_key? I include? K key?, keys M marshal_dump, marshal_load N new S set, size T to_a, to_hash, to_xml V values Included Modules Enumerable Constants CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict] MESSAGE_OPTIONS = [:message] Attributes [R] details [R] messages Class Public methods new(base) Link Pass in the instance of the object that is using the errors object. class Person def initialize @errors = ActiveModel::Errors.new(self) end end Source: show | on GitHub # File activemodel/lib/active_model/errors.rb, line 72 def initialize(base) @base = base @messages = apply_default_array({}) @details = apply_default_array({}) end Instance Publ
a vague awareness that strings meant to be displayed to users can be stored outside of models, controllers, and views). I will explain why Rails's validation messages are incorrect, and I will provide a simple monkey-patch which can let you write less restricted validation messages. In the process, you should become more comfortable with translations and less comfortable with several Rails methods. Rationale There is a golden rule in i18n ("internationalization"): do not use string concatenation. Why String Concatenation Breaks… An example: I once worked on a website which lists organizations in both English and Swahili. In English, one could write, "found 16 companies"; in Swahili, this might translate to, "masharika 16 yamekutanwa" (if I studied my grammar correctly). A programmer might be tempted to write: s = I18n.t('Found') + ' ' + number + ' ' + I18n.t('company', :count => number) with a translations file containing en: company: one: company other: companies found: found But in Swahili, the best this strategy could produce is, "Tumeyakutana 16 masharika," which is completely out of order. Instead, our intrepid programmer should write: s = I18n.t('found n companies', :count => number) with a translations file containing en: found n companies: zero: found no companies one: found one company other: 'found {{count}} companies' In English, the output is identical. At first glance, one might find the translation file repetitive (and clearer to read). But the real gain is that our program can now be translated to any language (depending on the I18n backend). We can now write a Swahili version of the string (excuse my grammar if I get these wrong): sw: found n companies: zero: masharika sifuri yamekutanwa one: sharika moja limekutanwa other: 'masharika {{count}} yamekutanwa' Problem solved! And we learned a valuable moral: do not use string concatenation to build sentences. … Even in a Single Language Another example: suppose an