プログラミングマガジン

プログラミングを中心にIT技術をできるだけわかりやすくまとめます。

  • ホーム
  • Ruby on Rails
  • 【Ruby on Rails】ページごとに、読み込まれるCSSとJavaScriptを変え…
 
 
     
  • サーバー言語  
    • Python
    • Ruby
    • PHP
    • SQL
  •  
  • インフラ  
       
    • AWS
    •  
    • 基本
    • Git
  • Web
       
    • Web開発
    • JavaScript
    • Vue.js
    • React
  •  
  • 設計  
       
    • 実装設計
    • DB設計
  • 問い合わせ
  

【Ruby on Rails】ページごとに、読み込まれるCSSとJavaScriptを変えるには?(Rails5まで)

06.20

  • miyabisan2
  • コメントを書く

この記事は2分で読めます

Railsアプリでは、デフォルトでアセットに含まれているCSSとJavaScriptは全て読み込むようになってしまっています。

ただ、現実としてはページごとに読み込まれるCSSとJavaScriptを変更したいということがほとんどでしょう。

作業を行う前に、事前にアセットについて不安な方は下記の記事をご確認下さい。

【Ruby on Rails】アセット(CSS,JavaScript)やマニフェストファイルの仕組み(Sprockets、Webpacker)

下記の手順で行います。

1.マニフェストファイルから、require_treeの記述を削除する。

「require_tree」は、全てのCSS、JavaScriptを読み込むという意味になっていたので、個別に変えたい場合は不要となります。

css

(Railsアプリ名)/app/assets/stylesheets/application.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* 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

(Railsアプリ名)/app/assets/javascripts/application.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree . ★削除する。

2.アセット用の定義ファイルに個別で読み込みたいCSSとJavaScriptを追加します。

アセット用の定義ファイル

1
(Railsアプリ)/config/initializers/assets.rb

このファイルの設定は全ての環境で読み込まれます。下記の記述を追加します。

1
Rails.application.config.assets.precompile += %w( test.js test.css )

それぞれ下記のファイルが読み込み対象となっています。

ファイルの種類 説明
スタイルシート (Railsアプリ名)/app/assets/stylesheets/test.scss
JavaScript (Railsアプリ名)/app/assets/javascripts/test.coffee

3.読み込ませるアセットファイル(SCSS、CoffeeScript)の内容を編集します。

それぞれ、下記のようなファイル内容として用意します。

css

(Railsアプリ名)/app/assets/stylesheets/test.scss

1
2
3
4
5
6
7
// Place all the styles related to the test controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
 
h1{
  color:red;
}

JavaScript

(Railsアプリ名)/app/assets/javascripts/test.coffee

1
2
3
4
5
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
 
alert('Hello World!');

4.ビュー側のファイルに、読み込むアセットを設定する。

(Railsアプリ名)/app/views/test/index.html.erb

1
2
3
4
<%= stylesheet_link_tag 'test' %>
<%= javascript_include_tag 'test' %>
 
<%= @helloworld %>

下記のように、定義ファイルに定義したスタイルシートと、JSをビューにインポートします。

以上で完了です。

動作検証

jsが適用されている。

cssが適用されている。

スポンサーリンク
  • 2018 06.20
  • miyabisan2
  • コメントを書く
  • Ruby on Rails
  • Tweets Twitter
  • このエントリーをはてなブックマークに追加
  • LINEで送る

関連記事

  1. 2020 11.17

    【Ruby on Rails】「主キー」、「複合キー」、「composite_primary_keys」について

  2. 2019 11.23

    【Ruby on Rails】Bundler、Gemfileについて

  3. 2019 12.01

    【Rspec】itのマッチャの種類(カスタムマッチャ、aggregate_failuresなども)

  4. 2020 07.18

    【Ruby on Rails】「ActionText」のリッチテキストを使う

  5. 2019 12.07

    【Ruby on Rails】「ページング」の実装(kaminari)

  6. 2019 12.09

    【Ruby on Rails】「コントローラ」の共通化

  • コメント ( 0 )
  • トラックバック ( 0 )
  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

返信をキャンセルする。

【Ruby on Rails】アセット(CSS,Jav…

【Python】「Anaconda」で「Django」…

RETURN TOP

著者プロフィール

エンジニア歴10年で過去に業務系、Webデザイン、インフラ系なども経験あります。現在はWeb系でフロントエンド開発中心です。

詳細なプロフィールはこちら

スポンサーリンク

カテゴリー

  • Android
  • AngularJS
  • API
  • AWS
  • C++
  • CSS
  • cursor
  • C言語
  • DDD
  • DevOps
  • Django
  • Docker
  • Figma
  • Git
  • GitLab
  • GraphQL
  • gRPC
  • Hasura
  • Java
  • JavaScript
  • Kubernetes
  • Laravel
  • linux
  • MySQL
  • Next.js
  • nginx
  • Node.js
  • NoSQL
  • Nuxt.js
  • Oracle
  • PHP
  • Python
  • React
  • Redux
  • Rspec
  • Ruby
  • Ruby on Rails
  • Sass
  • Spring Framework
  • SQL
  • TypeScript
  • Unity
  • Vue.js
  • Webサービス開発
  • Webデザイン
  • Web技術
  • インフラ
  • オブジェクト指向
  • システム開発
  • セキュリティ
  • その他
  • データベース
  • デザインパターン
  • テスト
  • ネットワーク
  • プログラミング全般
  • マイクロサービス
  • マイクロソフト系技術
  • マルチメディア
  • リファクタリング
  • 副業
  • 未分類
  • 業務知識
  • 生成AI
  • 設計
  • 関数型言語
RETURN TOP

Copyright ©  プログラミングマガジン | プライバシーポリシー