简体   繁体   中英

Place HTML in a Rails Helper

So i have an input for a simple_form like this:

<%= f.input :preferred_locale, collection: languages_available, label: "Choose Language", selected: current_user.preferred_locale, include_blank: false %>

So this creates a dropdown with languages available in the project. Languages available is an array like this, placed in a rails helper (extension.rb, only ruby, file):

def languages_available
    [
      ["English", 'en'],
      ["Spanish", 'es'],
    ]
end

The thing is want to display an icon right next to the language displaying the country flag. Thing is, in helpers you cant write html. No problem, i know the existence of html_safe, so in the collection i did something like:

<%= f.input :preferred_locale, collection: languages_available.map { |e| e.map(&:html_safe) }, label: "Choose Language", selected: current_user.preferred_locale,include_blank: false %>

So this marks as html_Safe every substring. With this i never see anything, and i am trying something as simple as putting in the array

def languages_available
    [
      ["<b>English</b>", 'en'],
      ["Spanish", 'es'],
    ]
end

the thing is something is working since i only see "English" and not "< b> English < /b>" as plaint text, but im not seeing the word in bold font so i dont know whats wrong.

edit: thank you for the answers, seems like in collection, which generates a select tag, is not possible to use html since it only supports text, i'll have to find another way of accomplishing this.

I don't think that you can do it in Html, options are not customizable, but maybe u can use a list, and with css and/or js customize to do a dropdown, I know that exist a lot of libraries to do it.

Here you have an example: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button

Another thing maybe is use emojis with the flags, I never tried but maybe you can put it plain text and with some library automatically convert it to an emoji.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM