1

I'm a little confused even after going through the Rails Asset Pipeline tutorial. I'm trying to add the latest jquery files. Do I do this through the layout javascript_include_tag or through the application.js ?

If so what is the difference and how would I specify the actual build number if I was using //= require jquery in the application.js

<%= javascript_include_tag "http://ajax.googleapis.com/ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js",
 "jquery.rails.js", 
 "application.js" 
 %>

The above method is adding jquery twice once with the above line and then through application.js

//= require jquery 

Is the application require getting the js file from the gem that's installed. In my Gemfile I have the line

gem 'jquery-rails'

So my main question is where should I be loading my specific build of jquery from.

4

2 に答える 2

2

jquery-railsには特定のバージョンの jquery が含まれており (バージョンは gem のバージョンによって異なります)、それが を使用するときに含まれるものです//= require jquery。これを行う場合、jquery を に含めないjavascript_include_tagでください。

jquery-railsgem に含まれているものとは異なるバージョンを使用したい場合は、それをそのままに設定javascript_include_tagし、//= require jquery指示を省略します。

もう少し説明すると、//= require構文はアセット パイプラインによって使用され、ロード時に特定のファイルをロードします (プリコンパイルされている場合はマージも行います) application.js。これの主な利点の 1 つは、1 つのリソース ファイルのキャッシュと配信が高速になることです。を使用して複数のスクリプトを個別に含めるjavascript_include_tagことは、スクリプトをロードする別の方法ですが<script>、ファイルごとに個別のタグを追加するため、//= require構文によって提供される利点が得られません。

ただし、CDN で提供されるスクリプトの場合、失われた利益はほとんど補われます。これは、クライアントによってすでにキャッシュされている可能性が高く、非常に迅速に提供され、将来変更されないことが保証されているためです。

于 2012-12-04T16:45:35.200 に答える
0

アプリケーション.js

ファイルは、本番環境では 1 つのファイルで提供 (圧縮およびコンパイル) されます。つまり、次のことを意味します。

//= require my_file

あなたは得る:

<script ... src="/assets/application.js" ...>

application.js に my_file を含み、圧縮されています。これは、デフォルトの本番構成です。

config.assets.compress = true # just to compress
config.assets.compile = false # false to use precompiled assets, you precompile with rake assets:precompile
                              # true to allow compilation in production

注: プリコンパイルとは、scss および coffee ファイルを css および javascript に変換して public ディレクトリに保存し、これらのファイルが要求されたときに Rails バックエンドにヒットしないようにすることです。

javascript_include_tag

javascript_include_tag 'application', 'my_second_file'

あなたは得る:

<script ... src="/assets/application.js" ... >
<script ... src="/assets/my_second_file.js" ... >

そのままにしておくとmy_first_file、application.js に埋め込まれます//= require my_first_file

どこに置くか

本番環境と同様に、デバッグはしませんよね?, それらをapplication.js.

于 2013-04-26T15:02:22.987 に答える