Rails Rails Model 验证,错误信息里面,不希望显示字段名 name,有何办法不?

hunkguo · 2013年09月25日 · 最后由 tumayun 回复于 2013年09月26日 · 3606 次阅读

使用默认的验证,显示的错误信息是 name 不能为空,而我只想显示 model 定义的错误信息,不希望显示字段名 name,有何办法不?网上的好多方法,有些可以用,但显示时总有些问题。

#1 楼 @ruby_sky simple_form 在尝试了,不知道如何生成重复输入的密码框。

自定义提示信息?建议参考下这个 http://guides.rubyonrails.org/active_record_validations.html#message

还有,simple_form 的 Readme 中有这样的用法:

<%= simple_form_for @user do |f| %>
  <%= f.input :username, label_html: { class: 'my_class' } %>
  <%= f.input :password, hint: false, error_html: { id: 'password_error'} %>
  <%= f.input :password_confirmation, label: false %>
  <%= f.button :submit %>
<% end %>

这个应该就是重复输入密码的了吧。 参见 https://github.com/plataformatec/simple_form

#3 楼 @chunlea 按 readme 中的写法试了,没效果,根本不验证。

#4 楼 @hunkguo 你的 model 里面怎么写的?

请在 environment.rb 中 ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| 重写该语句即可

昨半夜想起 simple_form 充分利用了 i18n 文件,尝试下,果然,在文件中定义好字段名,在错误信息显示的时候就正常了。

obj.errors.values
需要 登录 后方可回复, 如果你还没有账号请 注册新账号