自定义错误重复输入设计

时间:2017-12-14 10:23:59

标签: mysql ruby-on-rails ruby ruby-on-rails-4 devise

我想自定义已由设备

注册的用户错误消息

我的注册类别:

class Usuarios::RegistrationsController < Devise::RegistrationsController

 def new
  super do |usuario|
   redirect_to "" 
  end
end

def create
  super do |usuario|
    usuario.prevent_login = true
  end
end

def build_resource(hash=nil)
  super
  set attributes
end

1 个答案:

答案 0 :(得分:0)

您可以通过在帮助程序中添加devise_helper.rb来完成此操作。这将覆盖默认的Devise错误消息

module DeviseHelper
  def devise_error_messages!
  return "" unless devise_error_messages?

  messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
  sentence = I18n.t("errors.messages.not_saved",
                  :count => resource.errors.count,
                  :resource => resource.class.model_name.human.downcase)

  html = <<-HTML
  <div id="error_explanation">
  <h2>#{sentence}</h2>
  <ul>#{messages}</ul>
  </div>
  HTML

  html.html_safe
end

def devise_error_messages?
  !resource.errors.empty?
end

end

You can refer here