質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

446閲覧

webpackerでCSSを読み込ませる方法について

suzaku

総合スコア1

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/05/06 06:43

解決したいこと

現在個人アプリを作成しており、bootstrap4を導入しデザインを反映させることはできましたが
webpackerでcssを読み込ませることができません。

webpackerやアセットパイプラインについてrails guidesを調べてみたりはしつつコードを書いてみましたが,
しばらく悩んでいるためお力をお借りしたいです。

やったこと

  1. jquery bootstrap popper.jsをinstall
$ yarn add jquery bootstrap popper.js yarn add v1.22.10 [1/4] ???? Resolving packages... warning popper.js@1.16.1: You can find the new Popper v2 at @popperjs/core, this package is dedicated to the legacy v1 [2/4] ???? Fetching packages... [3/4] ???? Linking dependencies... warning " > webpack-dev-server@3.11.2" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0". warning "webpack-dev-server > webpack-dev-middleware@3.7.3" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0". [4/4] ???? Building fresh packages... success Saved lockfile. success Saved 3 new dependencies. info Direct dependencies ├─ bootstrap@4.6.0 ├─ jquery@3.6.0 └─ popper.js@1.16.1 info All dependencies ├─ bootstrap@4.6.0 ├─ jquery@3.6.0 └─ popper.js@1.16.1 ✨ Done in 3.86s.

現在のファイル配置

app/assets: ├── stylesheet: └── application.scss └── custom.scss
app/javascript: ├── packs: │ └── application.js └── src: │ └── application.scss └── stylesheets: └── application.css

ソースコード

assets/stylesheet/application.scss

/* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's * vendor/assets/stylesheets directory can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * *= require_tree . *= require_self */

javascript/packs/application.js

import 'bootstrap' import '../src/application.scss' import 'packs/application.css' import Rails from "@rails/ujs" import Turbolinks from "turbolinks" import * as ActiveStorage from "@rails/activestorage" import "channels" Rails.start() Turbolinks.start() ActiveStorage.start()

javascript/src/application.scss

@import '~bootstrap/scss/bootstrap';

javascript/stylesheets/application.css

@import '~bootstrap/scss/bootstrap';

views/layout/application.html.css

<!DOCTYPE html> <html> <head> <title>TaskApp</title> <meta name="viewport" content="width=device-width,initial-scale=1"> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <% if flash[:notice] %> <p><%= flash[:notice] %></p> <% end %> <%= yield %> </body> </html>

ファイルなどわかりづらい点を教えて頂ければすぐ追加情報を送るようにします。
レビューをよろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問