我正在使用Rails 3.2.9开发ROR应用程序,我在应用程序中收到注册页面的错误消息,如下所示
这些是Active Record Validation的默认消息。 (参考:http://guides.rubyonrails.org/active_record_validations_callbacks.html)
此应用程序之前已在Rails 2中编写,后来已迁移到rails 3.我已根据rails 3更改了validates_presence_of命令以验证: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获得以下代码,其中shud将在application_helper.rb中添加,并且html.erb文件中的相应更改为&lt;%= error_messages_for(@ user)%&gt;
代码:
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
答案 0 :(得分:1)
http://guides.rubyonrails.org/active_record_validations_callbacks.html
如果您阅读本指南,您会看到在您的表单中,您可以使用form.error_messages
答案 1 :(得分:0)
问题的解决方案添加在问题
下方