I’ve set up a small app that takes an email address and saves it, I’ve set up validation on the model (unique and valid email) and these both work.
I’m using the below code to try save the email, if it already exists or its not a valid format it needs to stop and set the error message
def create
interest = KnownInterest.new( :email => params[:email] )
if(interest.valid? and interest.save)
flash[:notice] = "Thanks for showing interest, We'll be in touch with updates."
else
flash[:notice] = interest.errors.messages
end
redirect_to action: "index"
end
this spits out [“Email not valid”], how do i get this to be a string (not what I think is an array, correct me if I’m wrong)
If you just want the first message then
interest.errors.messages.first. If you want them all then something likeinterest.errors.full_messages.join(", ")will group all the messages into one string.However you might want to brush up on
ActiveRecordvalidations and errors.Here’s a pretty good guide:
http://guides.rubyonrails.org/active_record_validations_callbacks.html
Read at least: