Skip to content

Instantly share code, notes, and snippets.

@paneq
Created December 28, 2011 18:06
Show Gist options
  • Save paneq/1528928 to your computer and use it in GitHub Desktop.
Save paneq/1528928 to your computer and use it in GitHub Desktop.
<% @user.errors[:login].each do |err| %>
<span class="help-block">
<p><%= error_message(err) %></p>
</end>
<% end%>
class User < ActiveRecord::Base
validates :login,
length: {
in: 2..10,
message: {
code: :INVALID_LENGTH,
max: 10,
min: 2,
description: "Invalid length"
}
}
end
module UsersHelper
def error_message(error)
return error if error.is_a?(String)
method = error[:code].downcase + "_error_message"
return send(method, error) if respond_to?(method) # We have a special way of displaying such error
return error[:description] # Just display the description from model
end
def invalid_length_error_message(error)
"length of this text is invalid. Minimum is <strong>#{error[:min]}</strong> and maximum is <strong>#{error[:max]}</strong>".html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment