2

I have a model,group_question_answer.rb

class GroupQuestionAnswer < ActiveRecord::Base
  belongs_to :group_question
  validates_presence_of :answer 
  validates_presence_of :answer_question    

end

for attribute answer and answer_question i get error message as Group question answers answer can't be blank I need to show only answer cant be blank.i even tried adding :message=>"cant be blank",but still i dont get my required message.how can i remove model name and can just arrtibute error message ....

Milind
  • 4,535
  • 2
  • 26
  • 58
  • 1
    Did you checked [this](http://stackoverflow.com/a/2859275/664404)? The method is outlined [here](http://edgeguides.rubyonrails.org/i18n.html#translations-for-active-record-models). – barerd Dec 28 '12 at 11:01

3 Answers3

5
class GroupQuestionAnswer < ActiveRecord::Base
  attr_accessible :answer
  validate do |group_question_answer|
    errors.add(:base, "answer can't be blank") if group_question_answer.answer.blank?
  end
end

works perfectly

rails c
Loading development environment (Rails 3.2.9)
irb(main):001:0> q = GroupQuestionAnswer.create
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
=> #<GroupQuestionAnswer id: nil, answer: nil, created_at: nil, updated_at: nil>
irb(main):002:0> q
=> #<GroupQuestionAnswer id: nil, answer: nil, created_at: nil, updated_at: nil>
irb(main):003:0> q.save
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
=> false
irb(main):004:0> q.errors
=> #<ActiveModel::Errors:0x007fc2fb325fa8 @base=#<GroupQuestionAnswer id: nil, answer: nil, created_at: nil, updated_at: nil>, @messages={:base=>["answer can't be blank"]}>
irb(main):006:0> q.errors.messages
=> {:base=>["answer can't be blank"]}

=> {:base=>["answer can't be blank"]}

irb(main):007:0> q = GroupQuestionAnswer.create(answer: "123")
   (0.1ms)  begin transaction
  SQL (9.0ms)  INSERT INTO "group_question_answers" ("answer", "created_at", "updated_at") VALUES (?, ?, ?)  [["answer", "123"], ["created_at", Fri, 28 Dec 2012 11:01:38 UTC +00:00], ["updated_at", Fri, 28 Dec 2012 11:01:38 UTC +00:00]]
   (1.1ms)  commit transaction
=> #<GroupQuestionAnswer id: 1, answer: "123", created_at: "2012-12-28 11:01:38", updated_at: "2012-12-28 11:01:38">
irb(main):008:0> q.errors.messages
=> {}

In my opinion validate method perfect way to fully customize rails validations and that does exactly what you ask for.

Eugene Rourke
  • 4,934
  • 1
  • 22
  • 24
3

You can try like this:

validates :answer, presence: { message: '<Your message>'}
validates :answer_question, presence: { message: '<Your message>'}
jizak
  • 382
  • 2
  • 13
  • Thanks jizak..i tried validates :answer, :presence=> { :message=> 'please answer'}.but still i am getting the same usual complete error..Group question answers answer please answer – Milind Dec 28 '12 at 10:52
  • 1
    This is due to the full_messages method. You can iterate through the validation errors like a hash `model.errors.each do |attr, message|` EDIT: the full_messages method automatically places the attribute name in front of the error message. By iterating using each you can completely omit the attribute name or choose to place it elsewhere. – Darren Coxall Dec 28 '12 at 12:47
0

Try adding them in your config/locales/en.yml file

As there you can do something like this,

en:
  errors:
    messages:
      answer: answer can't be blank
RoR Prog
  • 84
  • 2
  • 11