Rails Change Model Name Error Message
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 site About rails custom validation error message Us Learn more about Stack Overflow the company Business Learn more about hiring rails 4 validates presence message developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join rails custom validation method 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 Fully custom validation error message rails 4 validation error message with Rails up vote 191 down vote favorite 108 Using Rails I'm trying to get an error message like "The song field can't be empty" on save. Doing the following: validates_presence_of :song_rep_xyz, :message => "can't be empty" ... only displays "Song Rep XYW can't be empty", which is not good because the title of the field is not user friendly. How can I change the title
Rails Validation Message Without Field Name
of the field itself ? I could change the actual name of the field in the database, but I have multiple "song" fields and I do need to have specific field names. I don't want to hack around rails' validation process and I feel there should be a way of fixing that. ruby-on-rails share|improve this question edited Jun 14 '10 at 10:08 asked Apr 30 '09 at 19:05 marcgg 33.9k39153210 add a comment| 10 Answers 10 active oldest votes up vote 334 down vote accepted Now, the accepted way to set the humanized names and custom error messages is to use locales. # config/locales/en.yml en: activerecord: attributes: user: email: "E-mail address" errors: models: user: attributes: email: blank: "is required" Now the humanized name and the presence validation message for the "email" attribute have been changed. Validation messages can be set for a specific model+attribute, model, attribute, or globally. share|improve this answer edited Oct 17 '12 at 16:30 answered May 18 '10 at 16:43 graywh 6,30021923 16 If you are using mongoid, replace activerecord: with mongoid: –Intentss Nov 6 '11 at 14:07 83 @graywh: Where should questions about an answer be posted, if not
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 site About Us Learn more about Stack Overflow the company Business Learn more
Rails Validate Message
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users rails validation change attribute name Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping unknown validator: 'messagevalidator' each other. Join them; it only takes a minute: Sign up Changing Rails Active record column name 's error message up vote 1 down vote favorite I have created following page Rails model - insert a record - http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails Ruby way My issue is, the error message is not user friendly. It says "Cname can't be blank " cname is the column name. Instead of cname I want to show "column name cannot be blank" How do I do that ? Hope it is clear. As dimakura suggested class Contactu < ActiveRecord::Base validates :cname, presence: { message: 'Column name cannot be blank'} end . Cname Column name cannot be blank Cname can't be blank . In http://stackoverflow.com/questions/32529399/changing-rails-active-record-column-name-s-error-message view @contact_us.errors.messages.values.each do |error| %>
') that shows the same thing. –Khoga Sep 11 '15 at 18:30 use @contact_us.errors.full_messages.join('
') instead –dimakura Sep 11 '15 at 18:32 | show 1 more comment 2 Answers 2 active oldest votes up vote 1 down vote accepted Change it like this: class Contactu < ActiveRecord::Base validate :cname, presence: { message: 'user-friendly message' } validate :cdetails, presence: true end In order to hide field names, also use the following code to display your error messages: model.errors.full_messages.join('
') share|improve this answer edited Sep 11 '15 at 18:35 answered Sep 11 '15 at 17:58 dimakura 6,156826 Hi, Thanks for reply. Now it shows two errors and still prints the 'canme'. –Khoga Sep 11 '15 at 18:10 what's your script for showing messages
helpers. A minimal implementation could be: class Person # Required dependency for ActiveModel::Errors extend ActiveModel::Naming def initialize @errors = ActiveModel::Errors.new(self) end http://api.rubyonrails.org/classes/ActiveModel/Errors.html attr_accessor :name attr_reader :errors def validate! errors.add(:name, :blank, message: "cannot be nil") if name.nil? end # The following methods are needed to be minimally implemented def http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html 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 are required in your object for error message 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. The above allows you to do: person = Person.new rails custom validation 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 Public methods [](attribute) Link When passed a symbol or a name of a method, returns an array of errors
Record Validations and Callbacks Active Record Associations Active Record Query Interface Views Layouts and Rendering in Rails Action View Form Helpers Controllers Action Controller Overview Rails Routing from the Outside In Digging Deeper Active Support Core Extensions Rails Internationalization API Securing Rails Applications Debugging Rails Applications Performance Testing Rails Applications Configuring Rails Applications Rails Command Line Tools and Rake Tasks Asset Pipeline Extending Rails Rails on Rack Creating and Customizing Rails Generators Contributing to Ruby on Rails Contributing to Ruby on Rails API Documentation Guidelines Ruby on Rails Guides Guidelines Release Notes Ruby on Rails 3.2 Release Notes Ruby on Rails 3.1 Release Notes Ruby on Rails 3.0 Release Notes Ruby on Rails 2.3 Release Notes Ruby on Rails 2.2 Release Notes Contribute Credits Active Record Validations and Callbacks This guide teaches you how to hook into the life cycle of your Active Record objects. You will learn how to validate the state of objects before they go into the database, and how to perform custom operations at certain points in the object life cycle. After reading this guide and trying out the presented concepts, we hope that you'll be able to: Understand the life cycle of Active Record objects Use the built-in Active Record validation helpers Create your own custom validation methods Work with the error messages generated by the validation process Create callback methods that respond to events in the object life cycle Create special classes that encapsulate common behavior for your callbacks Create Observers that respond to life cycle events outside of the original class Chapters The Object Life CycleValidations OverviewWhy Use Validations? When Does Validation Happen? Skipping Validations valid? and invalid? errors[]Validation Helpersacceptance validates_associated confirmation exclusion format inclusion length numericality presence uniqueness validates_with validates_eachCommon Validation Options:allow_nil :allow_blank :message