0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails5で検索ボックスのplaceholderにFontAwesome5のアイコンを表示したい!

Posted at

やりたかったこと

  • ruby 2.6.3
  • rails 5.2.6
  • webpacker 5.3.0
  • Font Awesome 5 (yarnでインストール)

で,こんな風にして,検索フォームにFont Awesome 5 の虫眼鏡マーク(Unicodeはf002)を出したかった。

index.html.slim
= form_with url: search_path, method: :get, local: true do |f|
  = f.text_field :search, class: 'fa_icon', placeholder: "\uf002 検索ワードを入力"
  = f.submit "検索"                                        # ↑ここ!

でも,表示してみると文字化けして□が出てきてしまう…

解決方法

以下の記事(Rails6でしたが)を参考に解決しました!

app/javascript/stylesheets/application.scss
@import "~bootstrap/scss/bootstrap";
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
// 以下を追加しました。(追加したいアイコンはsolidだったので,これだけにしています。)
$fa-font-path: "~@fortawesome/fontawesome-free/webfonts";
@import "~@fortawesome/fontawesome-free/scss/solid";

// 中略

.fa_icon {
  font-family: "Font Awesome 5 Free";
  // weightは,使いたいフォントがSolidなら900,RegularやBrandsなら400,Lightなら300
  font-weight: 900;
}

ありがとうございました!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?