0

I have a model called 'dropbox', that extends 'programme'. Programme has the following validation:

validates_presence_of :network_id, :name

and so when you try to create a dropbox, leaving these out, you (unsurprisingly) get the following vaidation message:

* Network can't be blank
* Name can't be blank

Problem is, in the context of 'dropbox', we call 'network' 'category'. How can I change the network can't be blank error message to category can't be blank

---edit---

I've tried:

activerecord:
  attributes:
    dropbox:
      network_id: Category

but this has no affect. However, this works:

activerecord:
  attributes:
    programme:
      network_id: Category

but will change network's name everywhere (where as I just need it to be changed for dropbox). I believe this is because network_id is a property on programme and dropbox just extends this, but there must be a way round!

1 Answer 1

2

You could use the locales for that:

activerecord:
  attributes:
    dropbox:
      network: Category

Stick that in config/locales/en.yml to change the displayed names of the attributes. The create some error messages:

errors:
  messages:
    dropbox:
      cant_be_blank: Oops!

Then you add a message option to the model:

validates_presence_of :network_id, :name, :message => I18n.t('activerecord.errors.messages.dropbox.cant_be_blank')

Lots more info can be found here.

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

4 Comments

from that, I'm getting: ** Network Oops! Name Oops! ** Also, won't this mean that all Programmes (not just dropbox subclasses) will get the custom error message..?
I corrected the directory typo. Basically any attribute will use the names you enter there instead of their actual names (network_id or whatever else.) Take a look at the link I posted. Hopefully that will help you sort out the details.
The nesting of activerecord::errors::messages::dropbox gives you a set of messages for the dropbox model. When you want set up some for another model use activerecord::errors::messages::othermodel. Change the Oops to whatever you want your message to be.
I think I've worked out why this isn't working. network_id is a property on Programme, not on Dropbox. The only way I've been able to change it is by doing: activerecord: attributes: programme: network_id: Category, but this would obviously just change the name of network_id for all types of programme not just dropboxes. Doing activerecord: attributes: dropbox: network_id: Category has no affect.

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.