Railsでbootstrap-iconsを簡単に使う
2945 ワード
rails(webpacker)でbootstrap-iconsを使う方法
現時点ではv1.0.0-alpha5
が最新なので今後変わると思います。
npm
npm install bootstrap-icons
yarn
yarn add bootstrap-icons
次に app/helpers/application_helper.rb
に以下を追記します。
module ApplicationHelper
...
# 追加
def icon(icon, options = {})
file = File.read("node_modules/bootstrap-icons/icons/#{icon}.svg")
doc = Nokogiri::HTML::DocumentFragment.parse file
svg = doc.at_css 'svg'
if options[:class].present?
svg['class'] += " " + options[:class]
end
doc.to_html.html_safe
end
end
viewはslimを使用しているので
=icon("hdd", class: "text-gray")
で表示されます。
Author And Source
この問題について(Railsでbootstrap-iconsを簡単に使う), 我々は、より多くの情報をここで見つけました https://qiita.com/mammosu/items/035daaa9e1de3c3cebf2著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .