Search code examples
ruby-on-railsrubyunicodehamlsimple-form

Unicode character being escaped as plain text in simple_form with HAML


Because styling inputs is difficult I'm hiding the inputs in a simple_form and spoofing them with labels.

I'd like the label for the submit button to display a unicode character, but putting it in the second parameter escapes it as plain text.

With HAML:

= simple_form_for @post, html: {multipart: true} do |f|
        = f.file_field :file, {id: "file"}
        = f.button :submit, {id: "submit"}

        = label_tag(:file, "upload file")
        = label_tag(:submit, "&#8593")

Displays "upload file, &#8593"


Solution

  • If you don't want Rails to escape automatically, you can use raw(stringish) helper method.

    so it should be:

    = simple_form_for @post, html: {multipart: true} do |f|
      = f.file_field :file, {id: "file"}
      = f.button :submit, {id: "submit"}
    
      = label_tag(:file, "upload file")
      = label_tag(:submit, raw("&#8593"))
    

    FYI, it is not recommended if the data is coming from the user's input.