简体   繁体   English

ruby on rails只加载1个css&js文件

[英]ruby on rails only load 1 css & js file

I am new to ruby on rails and whenever i create and access a controller i can see it loads a new css & js file with name as the name of the controller... 我是ruby on rails新手,每当我创建和access controller我都可以看到它加载一个新的css & js文件,其名称为控制器的名称...

How can i make it load only 1 css and js file called style.css and site.js for example? 我怎么能只加载1个名为style.csssite.js css and js文件呢?

what is the purpose of having multiple js & css files whenever i access a contrller? 每当我访问一个控制器时,有多个js & css文件的目的是什么?

if i go to application.html.erb and change 如果我去application.html.erb并改变

  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>

to

  <%= stylesheet_link_tag("application") %>
  <%= javascript_include_tag("application") %>

it doesnt seem to help 它似乎没有帮助

I dont know which Rails version you are using, but assuming it is the recent one, then Rails are using Assets Pipeline, to merge multiple files and serve all css / javascript files compressed / merged. 我不知道你正在使用哪个Rails版本,但假设它是最近的版本,那么Rails正在使用Assets Pipeline,合并多个文件并提供压缩/合并的所有css / javascript文件。

You can learn more about it at http://guides.rubyonrails.org/asset_pipeline.html 您可以在http://guides.rubyonrails.org/asset_pipeline.html上了解有关它的更多信息

Basically what you do is, you reference one file 'application' (css or js) and inside this file, you customize which files it should include. 基本上你所做的是,你引用一个文件'application'(css或js),在这个文件中,你自定义它应该包含哪些文件。

The code: 编码:

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>

will include all .css and .js files available in assets/javascripts/ and assets/stylesheets/ . 将包括assets/javascripts/assets/stylesheets/提供的所有.css.js文件。 So, if you want to include only the style.css and site.js just remove everything else. 因此,如果您只想包含style.csssite.js ,请删除其他所有内容。

Your assets/javascripts/ should have only the application.js and site.js . 您的assets/javascripts/应该只包含application.jssite.js

And your assets/stylesheets/ should have the application.css and style.css . 你的assets/stylesheets/应该有application.cssstyle.css

EDIT 编辑

`application.js and application.css are part of the asset pipeline . `application.jsapplication.css资产管道的一部分。

You can disable it if you want by adding this line in your application.rb 如果需要,可以在application.rb添加此行来禁用它

config.assets.enabled = false

You can also change the application.html.erb to include only your style.css and site.js : 您还可以将application.html.erb更改为仅包含style.csssite.js

<%= stylesheet_link_tag    "style", :media => "all" %>
<%= javascript_include_tag "site" %>

I hope it helps... 我希望它有所帮助......

Try using :cache => 'cache-display' 尝试使用:cache => 'cache-display'

<%= stylesheet_link_tag :all, :cache => 'cache/display' %>
<%= javascript_include_tag :all, :cache => 'cache/display' %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM