Zogimu
Zogimu

Reputation: 211

Rails adds DB column name to error message

I am trying to show different message than predefined one. And in my model file i added this line to check if that company exists in db. I am using Rails 5.2.2

class Company < ApplicationRecord
    validates :tax_no, :uniqueness => {message: "This tax number has already been used"}
end

And i got the result as :

Company tax no This tax number has already been used

How can i remove that "Company tax no" ?

Upvotes: 0

Views: 40

Answers (1)

ray
ray

Reputation: 5552

This depends how you display your error message.

You will get proper idea from below,

u = User.new

u.valid?
# => false

u.errors.messages
# {:email=>["This field is required.", "This field is required.", "This field is required."], :password=>["This field is required."]}

u.errors.full_messages
# => ["Email This field is required.", "Password This field is required."]

u.errors.messages[:email]
# => "This field is required."

You have to inspect your view part and use @company.errors.messages[:tax_no] if @company.valid? is false for tax_no

Upvotes: 1

Related Questions