【问题标题】:Rails 3 Active record validation error messages with unwanted/extra charactersRails 3 带有不需要/多余字符的活动记录验证错误消息
【发布时间】:2013-04-04 07:38:42
【问题描述】:

我正在使用 Rails 3.2.9 开发 ROR 应用程序,我在我的应用程序中收到注册页面的错误消息,如下所示

  • 登录太短(最少 3 个字符)
  • 电子邮件太短(最少 7 个字符)
  • 电子邮件无效
  • 密码不能为空
  • 密码太短(最少4个字符)
  • 密码无效
  • 密码确认不能为空

这些是 Active Record Validation 的默认消息。 (参考:http://guides.rubyonrails.org/active_record_validations_callbacks.html

这个应用程序之前是用 Rails 2 编写的,后来迁移到 rails 3。我已经根据 rails 3 将 validates_presence_of 命令更改为 validates : password 、 :presence=>true 等。 在视图 (signup.html.erb) 中,error_messages_for 正在呈现这些消息。它已从 rails 3 中弃用。 谁能告诉我在视图中需要使用什么来代替 error_messages_for 并且所有代码都需要相应地更改以正确获取错误消息。..

这是代码(不完整)

app/model 中的 user.rb

class User < ActiveRecord::Base
  has_many :excel_files         # One user may have many excel files
  has_one  :user_access_validity# One user may have one license period 

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  attr_accessible :login
  attr_accessible :email
  attr_accessible :password
  attr_accessible :password_confirmation
  attr_accessible :company


  #changes of 'validates' in accordance with rails 3:

  validates :login,  :presence => true, 
                        :length => { :within => 3..40}, 
                        :uniqueness => { :case_sensitive => false },
                        :format => { :with =>  /^([a-z_0-9\.]+)$/i },
                        :on => :create, 
                        :if => :is_login_entered?
  validates :email, :presence => true, 
                        :length => { :within => 7..100}, 
                        :uniqueness => { :case_sensitive => false },
                        :format => {:with => /^([a-z]+((\.?)|(_?))[a-z0-9]+@(mindtree.com|rvce.edu.in))$/i},
                        :on => :create,
                        :if => :is_email_entered? 
  validates :company, :presence => true,
                        :format => { :with =>/(mindtree|RVCE)/i},
                        :format => { :with => /^([a-z]+)$/i },
                        :on => :create, 
                        :if => :is_company_entered? 
  #validates_presence_of     :login, :email, :company
 on => :create, :if => :is_login_entered?


   validates :password, :presence => true,
                       :length => { :within => 4..40 },
                       :confirmation => true,
                       :format => { :with => /^([a-z0-9@!#\$]+)$/i },
                       :on => :create,
                       :if => :password_required?



  validates :password_confirmation, :presence => { :if => :password_required? }
  #validates_presence_of     :password_confirmation,      :if => :password_required? 


  before_save :encrypt_password

。 . .

在signup.html.erb中

    <font color=red>(Fields marked * are mandatory)</font><h3>Sign me up!</h3>

    <br>


   <span class='error'><%= error_messages_for (@user) %></span>
    <%= form_for :user do |f| -%>

    <p><label for="login"><span class='redcolor'>*</span>Login</label><br/>
    <%= f.text_field :login %></p>

    <p><label for="email"><span class='redcolor'>*</span>Email</label><br/>
    <%= f.text_field :email %></p>

    <p><label for="password"><span class='redcolor'>*</span>Password</label><br/>
    <%= f.password_field :password %></p>

    <p><label for="password_confirmation"><span class='redcolor'>*</span>Confirm Password</label><br/>
    <%= f.password_field :password_confirmation %></p>

    <p><label for="company"><span class='redcolor'>*</span>Company</label><br/>
    <%= f.text_field :company %></p>

    <p><%= submit_tag 'Sign up' %></p>
    <% end -%>

解决方案

http://www.rubydoc.info/github/edavis10/redmine/ApplicationHelper:error_messages_for 获得以下代码,应将其添加到 application_helper.rb 并在 html.erb 文件中相应更改为

代码:

def error_messages_for(*objects)
  html = ""
  objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
  errors = objects.map {|o| o.errors.full_messages}.flatten
  if errors.any?
    html << "<div id='errorExplanation'><ul>\n"
    errors.each do |error|
      html << "<li>#{h error}</li>\n"
    end
    html << "</ul></div>\n"
  end
  html.html_safe
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 validation


    【解决方案1】:

    http://guides.rubyonrails.org/active_record_validations_callbacks.html

    如果您阅读本指南,您会发现您可以在表单中使用form.error_messages

    【讨论】:

    • 实际上我想知道是否应该在 application_helper.rb 文件中进行任何更改,该文件具有方法 error_messages_for 的定义
    • 在rails 3中application_helper中没有这样的方法,如果你有一些手写代码我认为最好重命名它或者如果它是自动生成的你可以删除它
    • 我实际上正在开发一个已经开发的应用程序,我从 rubydoc.info/github/edavis10/redmine/… 获得了 error_messages_for 的以下代码并且它有效 更新了上面的代码
    【解决方案2】:

    问题的解决方案添加在问题下方

    【讨论】:

    • 为什么不添加到这个帖子?
    猜你喜欢
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2018-11-14
    • 2023-03-31
    • 2013-11-09
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多