我想在水平simple_form中使用图标而不是文本作为标签。但是,我无法将文本与图标分开
= simple_form_for @user, html: { class: 'form-horizontal' }, wrapper: :horizontal_form do |f|
# shows 'username' label only
= f.input :username
# shows icon with 'username'
= f.input :username, label_html: {class: "fas fa-user"}
# shows nothing
= f.input :username, label_html: {class: "fas fa-user"}, label: false
wrapper_html:
和label_html:
很复杂,所以我只是手动完成
.row
.col-3
.text-right
i.fas.fa-user
.col-9
= f.input_field :username, label: false, class: 'form-control'
对于那些想要做同样事情的人来说,有一种更简单/更清洁的方式:
= f.label :username, label: ('<i class="fas fa-user"></i>').html_safe
= f.text_field :username
也适用于I18n本地化文件:
(en.yml)
username: '<i class="fas fa-user"></i>'
然后
= f.label :name, label: t(:username).html_safe
= f.text_field :username
使用simple_form(4.1.0)
希望这有帮助。