lucas clemente
lucas clemente

Reputation: 6389

Escaped characters in rails form

I'm having trouble using special characters with &-notation in ruby on rails forms. Have a look:

<% form_for(@object) do |f| %>
  <%= f.select :field, [['option 1', 1], ['  option 1.1', 2]] %>
<% end %>

As you see, option 1.1 should have two whitespaces in the front, it should look indented in the dropdown list. Being HTML, this does not work, I should use &nbsp;. How do I do this in this case?

Upvotes: 0

Views: 915

Answers (1)

Zabba
Zabba

Reputation: 65467

If your aim is to visually display indentation only, use CSS and set a class on the relevant options:

<%= f.select :field, [['option 1', 1],  ['option 1.1', 2, { :class=>'indent_level_1' }]] %>   

<style>
  .indent_level_1
  {
    color:red; /*just for testing whether the class got applied or not.*/
    margin-left:1em;
  }
</style>

If &nbsp; is supposed to be part of the text (I guess not?), then you can use raw():

<%= f.select :field, [['option 1', 1],  [raw('&nbsp;&nbsp;option 1.1', 2)]] %>   

Ps. Proper name for "&-notation" is "HTML character entity references"

Upvotes: 1

Related Questions