0

I'm working on ruby and i have the following line:

error = new_user.errors.to_json

if I do puts error the following json would be printed: {"email":["is invalid"]} and I want is to show the following text "the email entered is invalid" so how can I access both key and value from json?

thanks

12
  • Are you using Ruby on Rails? Commented Feb 12, 2014 at 13:10
  • yes, sorry for not mentioning it before Commented Feb 12, 2014 at 13:11
  • It seems to be ActiveRecord Validation errors attribute. At model level validation, you can use 'message' option and set your custom error message. validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => "The email entered is invalid" Commented Feb 12, 2014 at 13:19
  • could you enter a code example for me to understand it better? Commented Feb 12, 2014 at 13:21
  • if you want to print error message in view, then take a look at this link guides.rubyonrails.org/… Commented Feb 12, 2014 at 13:21

1 Answer 1

1

if you want to print the error messages, my suggestion is to try like this,

new_user.errors.full_messages.each do |error| 
  puts error 
end

to know more about ActiveRecord errors take a look the links ActiveRecord::Errors, Working with Validation Errors

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.