4
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 5 years have passed since last update.

【Rails】CSSやJavaScriptを読み込む小さなメモ

Last updated at Posted at 2018-04-23

デフォルトのマニフェストファイル(application.css,application.js)にある記載

●CSS

*= require_tree . ★全てのアセットを読み込む
*= require_self ★自身も読み込む

●js

//= require rails-ujs ★Railsが提供するJavaScriptヘルパ
//= require turbolinks ★Ajax化する仕組み
//= require_tree .★全てのアセットを読み込む

上記だと、 require_tree によってapp/assets/[js or css]/下の全てのファイルが読み込まれてしまうため、treeの記載を削除し、個別に定義する定義ファイル( config/initializers/assets.rb )に読み込むアセットを指定する(↓は test.js test.css の場合。scssにするならscssで指定)

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

ビューで以下を定義

    <%= stylesheet_link_tag    'test' %>
    <%= javascript_include_tag 'test' %>
4
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
4
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?