Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在这种情况下,如何创建更有意义的错误消息?_Ruby On Rails - Fatal编程技术网

Ruby on rails 在这种情况下,如何创建更有意义的错误消息?

Ruby on rails 在这种情况下,如何创建更有意义的错误消息?,ruby-on-rails,Ruby On Rails,假设我有以下模型: class Information < ActiveRecord::Base ... validates_length_of :name, :minimum=>3, :message=>"should be longer than 3 characters!" ... 类信息3、:message=>“的长度应大于3个字符!” ... 我想要的错误是: 信息长度应超过3个字符!(或类似产品) 而不是“信息名称应超过3个字符!” 我已经研究了两种可能的解决方

假设我有以下模型:

class Information < ActiveRecord::Base
...
validates_length_of :name, :minimum=>3, :message=>"should be longer than 3 characters!"
...
类信息3、:message=>“的长度应大于3个字符!”
...
我想要的错误是:
信息长度应超过3个字符!(或类似产品)
而不是“信息名称应超过3个字符!”

我已经研究了两种可能的解决方法:

  • human\u attribute\u name
    method(已提及):不适用于我的Rails 2.3.2:-(
  • 如果information.name.length<3,则直接执行
    information.errors.add”“,”…”:但是,这会删除由
    validated\u length\u方法触发的许多有用属性,如特殊类标记(用于将内容染成红色)

  • 有什么想法吗?谢谢您的时间。

    不要使用rails帮助程序出错,通常我会有内联错误,例如:

    def inline_error_block(obj, meth, prepend="", append="", klass="error error-form", &block)
      content = capture(&block)
      obj = (obj.respond_to?(:errors) ? obj : instance_variable_get("@#{obj}"))
      if obj
        errors = obj.errors.on(meth.to_s)
        if errors
          output = content_tag(:div, :class => klass) do
            content_tag(:p, "#{prepend}#{errors.is_a?(Array) ? errors.first : errors}#{append}", :class => "error-msg clearfix") + content
          end
          return concat(output)
        end
      end
      concat(content_tag(:div, content, :class => "no-error"))
    end
    
    倾向于这样做,但是,每个表单字段只显示一个错误,如果需要,我相信您可以重新排列以显示所有错误!(errors.first to errors.each)

    要获取全名,只需使用您希望显示的字段名编写消息:

    validates_length_of :name, :minimum=>3, :message=>"Information should be longer than 3 characters!"
    

    不要使用rails帮助器出错,通常我会有内联错误,例如:

    def inline_error_block(obj, meth, prepend="", append="", klass="error error-form", &block)
      content = capture(&block)
      obj = (obj.respond_to?(:errors) ? obj : instance_variable_get("@#{obj}"))
      if obj
        errors = obj.errors.on(meth.to_s)
        if errors
          output = content_tag(:div, :class => klass) do
            content_tag(:p, "#{prepend}#{errors.is_a?(Array) ? errors.first : errors}#{append}", :class => "error-msg clearfix") + content
          end
          return concat(output)
        end
      end
      concat(content_tag(:div, content, :class => "no-error"))
    end
    
    倾向于这样做,但是,每个表单字段只显示一个错误,如果需要,我相信您可以重新排列以显示所有错误!(errors.first to errors.each)

    要获取全名,只需使用您希望显示的字段名编写消息:

    validates_length_of :name, :minimum=>3, :message=>"Information should be longer than 3 characters!"
    

    我认为您可以通过full_messages方法显示错误,该方法用于控制台,而不是web应用程序。您应该使用error_messages_on或error_messages_作为帮助程序(有关更多信息,请参阅),这允许您自定义错误消息

    例如:

    <%= error_message_on "information", "name", :prepend_text => 'Information ' %>
    
    ”信息“%”
    
    我认为您可以通过完整消息方法显示错误,该方法是用于控制台,而不是用于web应用程序。您应该使用错误消息打开或错误消息作为帮助程序(有关更多信息,请参阅),这允许您自定义错误消息

    例如:

    <%= error_message_on "information", "name", :prepend_text => 'Information ' %>
    
    ”信息“%”
    
    您可以在模型中将:message设置为空字符串,然后在视图中将:prepend_文本设置为您喜欢的内容。

    您可以在模型中将:message设置为空字符串,然后在视图中将:prepend_文本设置为您喜欢的内容。

    谢谢您的帮助。在我的系统中,底线显示为:Info信息名称信息应超过3个字符!:(感谢您的帮助。在我的系统上,底线显示为:信息名称信息应超过3个字符!:(