9

I have an error message that appears when my the field for the database :b_name is empty. However, b_name stands for Business Name and I have made the label say that. However, when I get the error message, it says B name cant be blank. Is there any way I can change it so when I get the error it says Business Name can't be blank instead of b_name cant be blank?

Jake
  • 957
  • 1
  • 10
  • 12

3 Answers3

13

Yes it is actually really simple.

You should have a file named config/locales/en.yml, if not simply create one. There you can add your own custom names.

en:
  activerecord:
    models:
      order:            "Order"
    attributes:
      order:
        b_name:         "Business Name"

That will replace your b_name for "Business Name"

Your Order model in app/models/order.rb should look like:

class Order < ActiveRecord::Base
  validates :b_name, :presence => true
  .
  . 
  .

Please let me know if it worked :)

Here is an screenshot of my app working fine. Here is an screenshot of my app working

rogeliog
  • 3,632
  • 4
  • 27
  • 26
  • Its not working but maybe I'm doing something wrong. My model name is Order so heres what mine looked like. – Jake Jun 24 '11 at 21:57
  • en: models: model_name: "Order" attributes: model_name: b_name: "Business Name" other_attr: "Other Name" – Jake Jun 24 '11 at 21:57
  • i think what i have to do is in my application controller do before_filter :set_locale and make a function set_locale. Am I on the right track? – Jake Jun 24 '11 at 22:04
  • Ups my bad, it should be nested under active record, let me fix it. It should work now :) – rogeliog Jun 25 '11 at 00:25
  • i'm not getting an error but I'm not getting success. It still says B name can't be blank. Any thoughts? – Jake Jun 25 '11 at 01:09
  • The yaml file is tab sensitive, so you should be sure that your file is indented correctly, just as shown on the code above. I created a new Rails application to replicate your situation and it is working for me, let my share you my code :) – rogeliog Jun 25 '11 at 01:24
  • I encourage you to put the tabs exactly as mines. – rogeliog Jun 25 '11 at 01:33
  • I'm so sorry I didn't know the tabs were case sensitive like that. Thank you so much! – Jake Jun 25 '11 at 01:42
3

Try using the :message validation option, it's common to all validation methods.

s.m.
  • 7,895
  • 2
  • 38
  • 46
0

My guess is you have something like this in your model

validates_presence_of :b_name

If you want to change the message printed when this validation fails you can use the :message option

validates_presence_of :b_name, :message => 'some other message' unfortunately this will still print the field name. The message will be 'B name some other message'

To get around it see this other SO question.

Using Rails validation helpers :message but want it without listing the column name in message

From this post The easiest method is to add a human_attribute_name method like so,

class User < ActiveRecord::Base

  HUMANIZED_ATTRIBUTES = {
    :b_name => "Business Name"
  }

  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end

end
Community
  • 1
  • 1
diedthreetimes
  • 4,086
  • 26
  • 38
  • so how would i do validates_presence_of :b_name now? would it be like validates_presence_of human_attribute_name("b_name") ? – Jake Jun 24 '11 at 22:29
  • No the validated presence of call wouldn't change – diedthreetimes Jun 24 '11 at 22:33
  • I get an error saying wrong number of arguments (2 for 1) on line #3. `

    <%= pluralize(object.errors.count, "error") %>`

    – Jake Jun 25 '11 at 01:20