目前,我的应用程序包含以下代码,用于在 model.rb 的视图上显示错误消息,但它没有像error_messages_for
Rails 3 中已弃用的那样更新代码。谁能建议我如何在 Rails 3 中做同样的事情?我希望逻辑在 model.rb 文件中,从那里我应该能够在视图中显示错误消息
excel_file.rb(model.rb)
#jus some sample function
def show_error(test_file)
if test_file == 'Upload Test Case'
errors[:base] << "Please upload excel sheet with testsuite config sheet and testcases"
elsif test_file == 'Upload Test Data'
errors[:base] << "Please upload excel sheet with test data"
end
end
sampleview.html.erb
#some code..
<span class='error'><%= error_messages_for (@excel_file) %></span>
#some code..
application_helper.rb
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