0
0

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.

ページ単位のjavascript読み込み

Last updated at Posted at 2016-12-05

app/assets/javscripts/application.jsの修正

以下のコードの削除

application.js
//= require_tree .

共通のjavascript配置
common.jsの呼び出し

application.js
//= require common

コントローラ/アクション.js 形式でjavascriptの配置

app/assets/javascripts/top/index.js とか
app/assets/javascripts/top/create.js とか

views/layouts/application.html.erb の修正

<!DOCTYPE html>
<html>
<head>
  <title>TestJs</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>

<%= javascript_include_tag "#{params[:controller]}/#{params[:action]}" if Rails.application.assets.find_asset("#{params[:controller]}/#{params[:action]}.js") %>

</head>
<body>

<%= yield %>

</body>
</html>

config/initializers/assets.rb の追加

Rails.application.config.assets.precompile += %w( top/index2.js )
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?