使用Font的Rails简单表单输入-太棒了 [英] Rails Simple Form input with Font-Awesome

查看:8
本文介绍了使用Font的Rails简单表单输入-太棒了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在水平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

推荐答案

对于那些想要这样做的人,有一种更简单/更干净的方法:

= 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)

希望对您有所帮助。

这篇关于使用Font的Rails简单表单输入-太棒了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆