0

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

    <li>登录太短(最少 3 个字符)</li><li>电子邮件太短(最少 7 个字符)</li><li>电子邮件无效</li><li>密码可以 x27;不能为空</li><li>密码太短(最少4个字符)</li><li>密码无效</li><li>密码确认不能为空</li><li>密码无效</li><li>密码确认不能为空</li><li>密码无效</li>李>

这些是 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 并且所有代码都需要相应地更改以获取正确的错误消息。

这是代码(不完整)

应用程序/模型中的 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 文件中的相应更改为 <%= error_messages_for (@user) % >

代码:

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
4

2 回答 2

1

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

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

于 2013-04-04T07:44:35.470 回答
0

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

于 2013-04-17T06:15:07.610 回答