1

I am a novice ruby developer.

<%= select_tag "access", "<option>Read</option><option>Write</option>".html_safe, multiple: true, class: 'form_input'%>

'html_safe' <<< what is used for?

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117

1 Answers1

2

html_safe is here to actually use the HTML tags inside the string as actual HTML.

For example:

  • "<p>Hello</p>".html_safe will actually print a HTML tag p wrapping the string "Hello"
  • "<p>Hello</p>" will output "<p>Hello</p>" inside the page (<p> tags not being evaluated as HTML)

In your case, "<option>Read</option><option>Write</option>".html_safe will output two option HTML tags with "Read" and "Write".


A better way to generate options for a select is ... options_for_select:

select_tag 'access', options_for_select(['Read', 'Write'])
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • 2
    It's worth mentioning that in unsafe mode, the default, then all HTML-specific characters are escaped to their entity equivalent. That is `

    ` becomes `<p>`.

    – tadman Sep 12 '14 at 17:44
  • wow...thank you so much!!@MrYoushiji Was understood thanks!! – Sookyeom Kim Sep 12 '14 at 19:42