Created
December 28, 2011 18:06
-
-
Save paneq/1528928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @user.errors[:login].each do |err| %> | |
<span class="help-block"> | |
<p><%= error_message(err) %></p> | |
</end> | |
<% end%> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
validates :login, | |
length: { | |
in: 2..10, | |
message: { | |
code: :INVALID_LENGTH, | |
max: 10, | |
min: 2, | |
description: "Invalid length" | |
} | |
} | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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