提交 403cce77 编写于 作者: J José Valim

Merge pull request #4828 from railsaholic/fix_error_message

fix has_one, has_many restrict error message
......@@ -74,7 +74,8 @@ def define_restrict_dependency_method
if dependent_restrict_raises?
raise ActiveRecord::DeleteRestrictionError.new(name)
else
errors.add(:base, :restrict_dependent_destroy, :model => name.to_s.singularize)
key = association(name).reflection.macro == :has_one ? "one" : "many"
errors.add(:base, :"restrict_dependent_destroy.#{key}", :record => name)
return false
end
end
......
......@@ -10,7 +10,9 @@ en:
messages:
taken: "has already been taken"
record_invalid: "Validation failed: %{errors}"
restrict_dependent_destroy: "Cannot delete record because dependent %{model} exists"
restrict_dependent_destroy:
one: "Cannot delete record because a dependent %{record} exists"
many: "Cannot delete record because dependent %{record} exist"
# Append your own errors here or at the model/attributes scope.
# You can define own errors for models or model attributes.
......
......@@ -1171,7 +1171,7 @@ def test_restrict_when_dependent_restrict_raises_config_set_to_false
assert !firm.errors.empty?
assert_equal "Cannot delete record because dependent company exists", firm.errors[:base].first
assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.companies.exists?(:name => 'child')
ensure
......
......@@ -184,7 +184,7 @@ def test_dependence_with_restrict_with_dependent_restrict_raises_config_set_to_f
firm.destroy
assert !firm.errors.empty?
assert_equal "Cannot delete record because dependent account exists", firm.errors[:base].first
assert_equal "Cannot delete record because a dependent account exists", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.account.present?
ensure
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册