Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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 跳过Rail 3中自定义标签\标记辅助程序中的HTML转义_Ruby_Ruby On Rails 3_Validation - Fatal编程技术网

Ruby 跳过Rail 3中自定义标签\标记辅助程序中的HTML转义

Ruby 跳过Rail 3中自定义标签\标记辅助程序中的HTML转义,ruby,ruby-on-rails-3,validation,Ruby,Ruby On Rails 3,Validation,我有一个很好的类ErrorFormBuilder,它允许我在表单视图的相应字段附近添加错误描述: class ErrorFormBuilder < ActionView::Helpers::FormBuilder #Adds error message directly inline to a form label #Accepts all the options normall passed to form.label as well as: # :hide_err

我有一个很好的类ErrorFormBuilder,它允许我在表单视图的相应字段附近添加错误描述:

    class ErrorFormBuilder < ActionView::Helpers::FormBuilder
  #Adds error message directly inline to a form label
  #Accepts all the options normall passed to form.label as well as:
  #  :hide_errors - true if you don't want errors displayed on this label
  #  :additional_text - Will add additional text after the error message or after the label if no errors
  def label(method, text = nil, options = {})
    #Check to see if text for this label has been supplied and humanize the field name if not.
    text = text || method.to_s.humanize
    #Get a reference to the model object
    object = @template.instance_variable_get("@#{@object_name}")

    #Make sure we have an object and we're not told to hide errors for this label
    unless object.nil? || options[:hide_errors]
      #Check if there are any errors for this field in the model
      errors = object.errors.on(method.to_sym)
      if errors
        #Generate the label using the text as well as the error message wrapped in a span with error class
        text += " <br/><span class=\"error\">#{errors.is_a?(Array) ? errors.first : errors}</span>"
      end
    end
    #Add any additional text that might be needed on the label
    text += " #{options[:additional_text]}" if options[:additional_text]
    #Finally hand off to super to deal with the display of the label
    super(method, text, options)
  end
end
无功而返

有没有办法绕过这种行为


谢谢

你试过让你的字符串html\u安全吗

irb(main):010:0> a = "A string"
=> "A string"
irb(main):011:0> a.html_safe?
=> false
irb(main):012:0> b = a.html_safe
=> "A string"
irb(main):013:0> b.html_safe?
=> true
请参阅并向下滚动至底部附近的“您需要知道的内容”:

一般来说,您可以像以前一样构建Rails应用程序。Rails将自动转义它未创建的任何字符串。在几乎所有情况下,这都是正确的行为,无需进一步修改

如果Rails正在转义一个您希望在不转义的情况下通过的字符串,只需将其标记为安全。如果在辅助对象中创建字符串,可能需要将其部分标记为安全

我无法测试这在您的子类助手中是否有效,但我认为是这样。

只需使用

super(method, text, options.merge({:escape => false}))
irb(main):010:0> a = "A string"
=> "A string"
irb(main):011:0> a.html_safe?
=> false
irb(main):012:0> b = a.html_safe
=> "A string"
irb(main):013:0> b.html_safe?
=> true