Rails 3.1 预编译控制器特定 JS 资源的策略

发布于 2024-12-04 19:16:25 字数 830 浏览 1 评论 0原文

为了将控制器特定的 JavaScript 逻辑排除在标准 application.js 之外,并且仅将其包含在相关控制器中,我将其放入自己的 .js 文件中,并根据控制器包含它布局中的名称如下:

<%= javascript_include_tag "application", params[:controller] %>

这工作得很好,但是当我将应用程序部署到生产环境时(我使用 Capistrano 并设置了预编译任务),资产管道不会预编译任何控制器特定的 JS文件。我认为这是因为 application.js 中的 require 指令没有引用我的实际 JavaScript 文件。

如何处理这个问题而不将控制器特定的 JS 移回 application.js,或者从 application.js 显式引用它?

有没有办法告诉资产管道预编译额外的列表文件?如何在生产环境中手动预编译特定文件?

更新

结果,您可以在此处指定单个文件config/environments/Production.rb:

config.assets.precompile += %w( achievements.js )

...或者我只是随意地为每个 JavaScript 文件添加它:

config.assets.precompile += %w( *.js )

In order to keep controller specific JavaScript logic out of the standard application.js and only have it included by the relevant controller, I'm putting it in its own .js file and including it based on the controller name from the layout like such:

<%= javascript_include_tag "application", params[:controller] %>

That works just fine, but when I deploy the app to production (I'm using Capistrano and have a pre-compile task set up), the asset pipeline doesn't precompile any of the controller specific JS files. I presume this is because my actual JavaScript file isn't referenced by require directives in application.js.

How do I deal with this without moving my controller specific JS back to application.js, or explicitly referencing it from application.js?

Is there some way to tell the asset pipeline to pre-compile an additional list files? How could I manually pre-compile a specific file on production?

Update

As it turns out, you can specify individual files here in your config/environments/production.rb:

config.assets.precompile += %w( achievements.js )

...or I just went ahead and capriciously added it for every JavaScript file:

config.assets.precompile += %w( *.js )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果您想预编译 js|css 仅在 asset/javascripts 和 asset/stylesheets 目录的根目录中找到(而不是它们的树层次结构),您可以将其放入环境文件中:

  Dir.chdir "#{Rails.root}/app/assets/javascripts"
  a = Dir.glob("*.{js,coffee,erb}")
  Dir.chdir "#{Rails.root}/app/assets/stylesheets"
  b = Dir.glob("*.{css,erb}")
  config.assets.precompile +=  a.concat(b)
  Dir.chdir Rails.root

If you want to precompile the js|css only found in the root of assets/javascripts and assets/stylesheets directories (and not their tree hierarchy), you can put this in environment files :

  Dir.chdir "#{Rails.root}/app/assets/javascripts"
  a = Dir.glob("*.{js,coffee,erb}")
  Dir.chdir "#{Rails.root}/app/assets/stylesheets"
  b = Dir.glob("*.{css,erb}")
  config.assets.precompile +=  a.concat(b)
  Dir.chdir Rails.root
榆西 2024-12-11 19:16:25

我认为你和 james_schorr 并不是真正在谈论同一件事。
您需要将application.js以外的文件添加到config.assets.precompile中。如果我没记错的话,他的回答更多的是关于您可以/应该采用的目录结构。

如果我想要特定于控制器,我会这样做:

/assets
    /javascripts
        /users
            login.js
            profile.js
        /blogs
        /posts
        users.js
        blogs.js
        posts.js

例如,users.js 将是:

*= require_tree ./users

这样,您可以保持组织性(每个控制器有很多 js 文件),但在产品中,它们将全部包含在一个文件中。

您的配置中仍然需要:

config.assets.precompile += %w( *.js )

I think you and james_schorr are not really talking about the same thing.
You need to add the files other than application.js to config.assets.precompile. His answer was more about the directory structure you could/should adopt, if I'm not mistaken.

If I wanted to have controller specific, I would do:

/assets
    /javascripts
        /users
            login.js
            profile.js
        /blogs
        /posts
        users.js
        blogs.js
        posts.js

And for instance, users.js would be:

*= require_tree ./users

That way, you can stay organized (have a lot of js files per controller), but in prod, they will all be included in one file.

Still need that in your config:

config.assets.precompile += %w( *.js )

这就是我所做的:

目录结构:

app/assets/javascripts/sessions/multiple.js
app/assets/application-sessions.js

application-sessions.js 只是有:

 *= require_self
 *= require_tree ./sessions

然后在您看来,

<% if @current_controller == 'whatever' %>
   <%= javascript_include_tag "application-sessions" %>
 <% else %>
   ….
 <% end %>

在我的 application_controller.rb 方法中执行 FYI,@current_controller =controller_name,使用 before_filter 调用。

This is what I do:

directory structure:

app/assets/javascripts/sessions/multiple.js
app/assets/application-sessions.js

application-sessions.js just has:

 *= require_self
 *= require_tree ./sessions

Then in your view, do

<% if @current_controller == 'whatever' %>
   <%= javascript_include_tag "application-sessions" %>
 <% else %>
   ….
 <% end %>

FYI, @current_controller = controller_name in my application_controller.rb methods, called with a before_filter.

他是夢罘是命 2024-12-11 19:16:25

我这里也遇到同样的问题,这让我很头疼。

包含 *.js 有点太多了,我不希望每个文件都编译成单独的文件,其中一些应该合并在一起。

我的想法是,将控制器特定文件存储到子目录中,例如“controllers”,然后仅包含controllers/*.js或css文件进行预编译。

不确定它是否有效,我会尝试一下,无论如何,这可能是一个有用的提示。

I am having same issue here, it's an headache to me.

Including *.js is a little too much, I don't want every files to be compiled into separated files, some of them suppose to be merged together.

My idea is, store the controller specific files into a sub directory, such as "controllers", and then only include controllers/*.js or css files for precompile.

Not sure if it work or not, I will try it out, anyway, it might be a useful hint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文