ok to remove some error messages Rails 3
I have a model as follows:
class Greeting < ActiveRecord::Base
attr_accessible :icon, :content, :name
belongs_to :user
belongs_t开发者_高级运维o :board
validates :user_id, :presence => true
validates :board_id, :presence => true
I don't want error messages displayed to the user if there is no user model or board model. I have done the following:
after_validation :remove_errors_for_user_board
def remove_errors_for_user_board
self.errors.delete_if{ |key,value| key == :user_id || key == :board_id }
end
So basically I remove any error messages for these attributes. Is this ok and is the best way to do this?
Thanks from a NOOB job.
I'm not quite understanding what you are saying. But if you are saying that you want there to be no errors if there is no user or no board. Then this will work…
validates_presence_of :name, :if => :board_or_user_is_present
def if_board_or_user_is_present
board.present? or user.present?
end
精彩评论