Rails: アセットにjquery-ui.min.cssを追加する

Rails 7.0のアセットにjQuery UIのCSSを追加する際の覚書です。

Yeah, you'll have to do this by hand. We don't have an API that could figure out what assets might be needed in addition to the main JS file. There may well be more than one. There may well be images. I don't see a path of us programmatically figuring this out.

What is the recommended way to import css bundled in js libraries · Issue #80 · rails-importmap-rail…
https://github.com/rails/importmap-rails/issues/80

環境
Ruby 3.0.6
Rails 7.0.8
jquery-ui.min.css 1.12.1

準備

rbenv install 3.0.6
rbenv global 3.0.6
gem install rails -v 7.0.8

rails _7.0.8_ new add_css
cd add_css
bin/rails db:migrate
bin/rails g controller home index
Rails.application.routes.draw do
  get 'home/index'
  root "home#index"
end

追加

mkdir -p vendor/assets/stylesheets
curl 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/le-frog/jquery-ui.min.css' --output 'vendor/assets/stylesheets/jquery-ui.min-1.12.1.css'

app/assets/stylesheets/application.css

 *= require_tree .
 *= require jquery-ui.min-1.12.1
 *= require_self

確認

以上です。

この記事が気に入ったらサポートをしてみませんか?