Rails Error Message Without Attribute
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings rails validation change attribute name and policies of this site About Us Learn more about Stack Overflow rails custom validation error message the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation rails change field name error message Tags Users Badges Ask Question 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
Rails Validation Field Name
only takes a minute: Sign up removing field name from validation error message [duplicate] up vote 7 down vote favorite 3 This question already has an answer here: Fully custom validation error message with Rails 10 answers in rails 3 i don't want to show field names in error messages. Anyone know how to do that ? validates_presence_of :title, :message rails error message without field name => "no title" it shows Title no title i want no title ruby-on-rails ruby-on-rails-3 share|improve this question asked Mar 21 '11 at 8:42 hakaooa 45113 marked as duplicate by Martijn Pieters♦ Sep 23 '15 at 17:05 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. That's not duplicate. –hlcs Sep 2 at 8:31 add a comment| 6 Answers 6 active oldest votes up vote 17 down vote In your form view change your current code <%@object.errors.full_messages.each do |msg| %>
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 rails human_attribute_name more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
I18n Ruby
Users Badges Ask Question 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 Validation error messages without the attribute up vote 3 down vote favorite 1 I'm trying to show custom error messages without the attribute name in http://stackoverflow.com/questions/5375407/removing-field-name-from-validation-error-message front. I used to do this with the custom_error_message gem, however it doesn't work with Rails 3.1 What I'm trying now in create.js.erb: alert("<%= @post.errors[:title] %>") Which returns ["Here goes my custom message?"] My question is -- how can I remove the brackets and the " so only the message is left. I'll insert it into the page using jquery. ruby-on-rails share|improve this question asked Jul 28 '11 at 17:44 Martin Petrov 1,05331736 add a comment| http://stackoverflow.com/questions/6863307/validation-error-messages-without-the-attribute 1 Answer 1 active oldest votes up vote 15 down vote accepted If you want errors on a base object, not a particular attribute use: errors.add(:base, "Here goes my custom message") As for the brackets and quotes, I guess it depends how you're setting your errors. When you just say @post.errors[:base] it will return an array. This alert is just literally spitting out the array. You'd probably want to iterate over the errors or just grab .first if there's only one. Also, calling .html_safe will take care of the " issue. share|improve this answer answered Jul 28 '11 at 17:50 brad 15.9k1981137 Much better framed answer than what I was typing. –rubish Jul 28 '11 at 17:52 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged ruby-on-rails or ask your own question. asked 5 years ago viewed 1685 times active 5 years ago Blog Stack Overflow Podcast #92 - The Guerilla Guide to Interviewing Related 191Fully custom validation error message with Rails0Omit attribute name from validation erro
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 http://stackoverflow.com/questions/690267/using-rails-validation-helpers-message-but-want-it-without-listing-the-column-n company Business Learn more about hiring developers or posting ads with us Stack Overflow https://makandracards.com/makandra/15343-tailor-rails-error-messages-for-models-and-attributes Questions Jobs Documentation Tags Users Badges Ask Question 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 Using Rails validation helpers :message but want it without listing the column name in message up vote 19 down error message vote favorite 6 Using Rails validation helpers- validates_xxx_of :the_column, :message => "my message" will generate a validation message : *the_column my message* Is there a way to turn-off the inclusion of the column name? (substitute xxx with any validation helper method) ruby-on-rails validation share|improve this question asked Mar 27 '09 at 15:40 daustin777 2,34551725 add a comment| 13 Answers 13 active oldest votes up vote 16 down vote I know this question is rails error message old. But just for reference in case someone else stumbles over it as I just did. At least in Rails 3.0.x (not sure about earlier versions) you can use the leading ^ as indicated by RadBrad without the need for any gems/plugins. share|improve this answer edited Jul 27 '12 at 6:24 answered Nov 24 '10 at 15:22 Hartwig 6131820 The question may be old but your answer was the one I was looking for. +1 –Brian Deterling Dec 4 '10 at 19:28 1 I'm using Rails 3.1 and this does not work. However, I can do: <% object.errors.full_messages.each do |msg| %> <% msg = msg.split("^").last %> ... <% end %> –tybro0103 Dec 31 '11 at 2:02 This does work for me in Rails 3.2.2 –Mitch May 22 '12 at 17:59 3 This does not work for me in Rails 3.2.6 –KenB Jul 26 '12 at 16:54 This didn't work for me (in rails 3.2.11), and it annoyed me a ton. I set it up to work using a helper. In case this is helpful to anyone: resource.errors.full_messages.map{ |m| m.split("^")[1] || m.split("^")[0] } –vansan Jul 10 '13 at 23:02 | show 1 more comment up vote 5 down vote Rails 4 answer Took a look at the code here: https://github.com/rails/rails/blob/mast
for models and attributes Rails has generic error messages you can define in your locale .yml files. You may override those application-wide error messages using model or attribute scope like this: en: activerecord: errors: messages: invalid: is invalid # used for any invalid attribute in the application models: car: invalid: does not work # used for invalid car attributes attributes: driver: invalid: not allowed to drive # used if the car's driver attribute is invalid Check out our new e-book: Growing Rails Applications in Practice Learn to structure large Ruby on Rails codebases with the tools you already know and love. Learn more Author of this card: Dominik Schöler makandra.de Say thanks Your thanks were sent to ! If you found our advice to be useful, you might like our book about maintainable Rails applications: Check out our new e-book: Growing Rails Applications in Practice Learn to structure large Ruby on Rails codebases with the tools you already know and love. Learn more About this deck: We are makandra and do test-driven, agile Ruby on Rails software development. License for source code License for source code All source code included in the card Tailor Rails error messages for models and attributes is licensed under the license stated below. This includes both code snippets embedded in the card text and code that is included as a file attachment. Excepted from this license are code snippets that are explicitely marked as citations from another source. The MIT License (MIT) Copyright (c) 2013-2016 makandra GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EV