自定义form提示

自定义form错误提示

application_controller.rb中添加:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  case html_tag
  when /<(label)/
    %(<div class="error">#{html_tag}</div>).html_safe
  when /<(input|textarea|select)/
    if instance.error_message.kind_of?(Array)
      %(#{html_tag}<dd class="error">#{instance.error_message.join('<br />')}</dd>).html_safe
    else
      %(#{html_tag}<dd class="error">#{instance.error_message}  </dd>).html_safe
    end
  end
end

创建:
shared/_error_messages.html.erb
<% if target.errors.any? %>
  <div id="error">
  <p><%= I18n.t('activerecord.errors.template.header', :count => target.errors.count, :model => target.class.model_name.human.downcase) %></p>
  </div>
<% end %>

调用:
<%= render "shared/error_messages", :target => @post %>

语言文件:
activerecord:models:post: "帖子"

你可能感兴趣的:(java,html,ActiveRecord)