如何为 rails select 表单助手中的选项设置颜色?
How to set a color for an option in a rails select form helper?
我在网上搜索了一下,看来我们可以通过在所需选项上应用 style: "background-color:colorName;"
来实现。
但经过多次研究和尝试,我找不到如何使用 rails 表单助手来完成此操作。
这就是我现在拥有的:
<% users_array = ["Choose your player"] %>
<% users_array += User.all.map { |user| [user.name, user.id ]} %>
<%= select("player-#{user.id}-#{category[0]}", nil, options_for_select(users_array, user.name.to_s ), { }, { class: "selectstyle col-md-2" }) %>
我使用 options_for_select
中指定的默认 selected 选项显示我所有的模型内容,但我想为这个 [=26] 中显示的模型的每个用户添加不同的颜色=].
我在网上找到的唯一示例是 select 没有模型显示,我的所有尝试都失败了。我该怎么办?
You can optionally provide HTML attributes as the last element of the array.
您可以这样做:
users_array += User.all.map do |user|
html_attributes = {}
html_attributes['style'] = '...' if <some_condition>
[user.name, user.id, html_attributes]
end
我在网上搜索了一下,看来我们可以通过在所需选项上应用 style: "background-color:colorName;"
来实现。
但经过多次研究和尝试,我找不到如何使用 rails 表单助手来完成此操作。
这就是我现在拥有的:
<% users_array = ["Choose your player"] %>
<% users_array += User.all.map { |user| [user.name, user.id ]} %>
<%= select("player-#{user.id}-#{category[0]}", nil, options_for_select(users_array, user.name.to_s ), { }, { class: "selectstyle col-md-2" }) %>
我使用 options_for_select
中指定的默认 selected 选项显示我所有的模型内容,但我想为这个 [=26] 中显示的模型的每个用户添加不同的颜色=].
我在网上找到的唯一示例是 select 没有模型显示,我的所有尝试都失败了。我该怎么办?
You can optionally provide HTML attributes as the last element of the array.
您可以这样做:
users_array += User.all.map do |user|
html_attributes = {}
html_attributes['style'] = '...' if <some_condition>
[user.name, user.id, html_attributes]
end