0

Is it possible to show validation error messages from controller by making our method ? please check the code below

validate :validation
  def validation
    if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil?
      #How do write my error message here ?
    end
  end

and my form

<% if @record.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@record.errors.count, "error") %> prohibited this record from being saved:</h2>

      <ul>
      <% @record.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

1 Answer 1

1

You can add errors to your instance using

self.errors.add(:base, "your message here")

You could put attributes name as symbol instead of :base or whatever you like.

In your case

if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil?
  self.errors.add(:base, "your message here")
end
3
  • so in the model in the if block i should write self.errors.full_messages ? Commented Jun 25, 2013 at 18:50
  • apparently something is wrong and it is not showing error message for any validation! Commented Jun 25, 2013 at 20:09
  • I answered your other thread. The code above is just to add new errors to your instance. Nothing to do with views or display mechanisms Commented Jun 25, 2013 at 20:13

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.