简体   繁体   English

将 HTML 放在 Rails 助手中

[英]Place HTML in a Rails Helper

So i have an input for a simple_form like this:所以我有一个 simple_form 的输入,如下所示:

<%= 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):可用语言是这样的数组,放置在 rails 助手中(extension.rb,仅 ruby,文件):

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.问题是,在助手中你不能写 html。 No problem, i know the existence of html_safe, so in the collection i did something like:没问题,我知道 html_safe 的存在,所以在集合中我做了类似的事情:

<%= 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.所以这将每个 substring 标记为 html_Safe。 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.事情是这样的,因为我只看到“英语”而不是“< b>英语</ b>”作为纯文本,但我没有看到粗体字,所以我不知道出了什么问题。

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.编辑:谢谢你的回答,似乎在收集中,它生成一个 select 标签,不可能使用 html 因为它只支持文本,我必须找到另一种方法来完成这个。

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.我不认为你可以在 Html 中做到这一点,选项不可定制,但也许你可以使用一个列表,并使用 css 和/或 js 定制做一个下拉,我知道有很多库可以做到这一点.

Here you have an example: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button这里有一个例子: 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.另一件事可能是使用带有标志的表情符号,我从未尝试过,但也许您可以将其放入纯文本并使用某些库自动将其转换为表情符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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