Rails 3 引擎和静态资产

发布于 2024-10-04 08:08:25 字数 1029 浏览 11 评论 0原文

我正在构建一个捆绑为 gem 的引擎(gmaps4rails)。我将引擎的 /public 复制到了 Rails 应用程序的 /public 中。

在开发中一切工作正常,但在生产中无法工作:似乎找不到静态资产(我的引擎和我的主应用程序)。

日志讲述了以下内容(只是摘要):

Started GET "/javascripts/application.js?1286294679" for 127.0.0.1 at Wed Nov 24 00:22:20 +0100 2010

ActionController::RoutingError (No route matches "/javascripts/application.js"):


Rendered /Users/me/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)


Started GET "/stylesheets/gmaps4rails.css?1290554221" for 127.0.0.1 at Wed Nov 24 00:22:20 +0100 2010

ActionController::RoutingError (No route matches "/stylesheets/gmaps4rails.css"):

我做了一些事情:

  1. 在我的应用程序的 production.rb 中,我设置了:

    config.serve_static_assets = true

    这解决了问题,但不够优雅,我想将其保留为 false 并在引擎中添加配置:)

  2. 我遵循了建议 这里 没有成功。

I'm building an engine I've bundled as a gem (gmaps4rails). I copied the /public of my engine in the /public of my rails app.

Everything works fine in development but fails to work in production: it appears static assets (of my engine & my main app) aren't found.

The logs tell the following (just an abstract):

Started GET "/javascripts/application.js?1286294679" for 127.0.0.1 at Wed Nov 24 00:22:20 +0100 2010

ActionController::RoutingError (No route matches "/javascripts/application.js"):


Rendered /Users/me/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)


Started GET "/stylesheets/gmaps4rails.css?1290554221" for 127.0.0.1 at Wed Nov 24 00:22:20 +0100 2010

ActionController::RoutingError (No route matches "/stylesheets/gmaps4rails.css"):

I've done a few things:

  1. in my app's production.rb, I set:

    config.serve_static_assets = true

    This Solves the problem but is not elegant enough, I'd like to keep it to false and add configuration in the engine :)

  2. I followed the advice here without success.

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

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

发布评论

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

评论(6

梦幻的心爱 2024-10-11 08:08:25

出于性能原因,静态资产服务在生产模式下被禁用。您的网络服务器应该配置为服务这些资产。

如果您正在使用,请查看此讨论 nginx 作为网络服务器。

For performance reason, static assets serving is disabled in production mode. Your webserver should be configured to serve theses assets.

Look at this discussion if your are using nginx as a webserver.

2024-10-11 08:08:25

在 Rails 3.x 中尝试在 config/environments/Production.rb 中设置它

config.serve_static_assets = true

默认情况下,Rails 假定您正在使用资产服务器(lighttp、nginx 或 Apache)

In Rails 3.x try to set this in config/environments/production.rb

config.serve_static_assets = true

By default Rails assumes you are using an assets server (lightttp, nginx or Apache)

葬﹪忆之殇 2024-10-11 08:08:25

在您的引擎中,将: 替换

initializer "static assets" do |app|
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end

为:

initializer "static assets" do |app|
      app.middleware.insert_before(::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public")
end

In your engine, replace:

initializer "static assets" do |app|
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end

with:

initializer "static assets" do |app|
      app.middleware.insert_before(::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public")
end
—━☆沉默づ 2024-10-11 08:08:25

您是否尝试过将其添加到 Rails::Engine 类中:

initializer "static assets" do |app|
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end

这将在运行时与应用程序合并到 Gem 的 /public 目录中。

Have you tried adding this to your Rails::Engine class:

initializer "static assets" do |app|
  app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end

This will merge in your Gem's /public directory with the app at runtime.

我不吻晚风 2024-10-11 08:08:25

我在安装 Rails 3.1 引擎时遇到了类似的问题。我在舞台和制作中收到了空白资产。

我在 http://jonswope.com/2010/07/25/rails-3-engines-plugins-and-static-assets/comment-page-1/#comment-87 并对其进行了调整适应Rails 3.1资产位置:

initializer "static assets" do |app|
  app.middleware.insert_before ::Rack::Lock, ::ActionDispatch::Static, "#{root}/app/assets"
end

我想有一种更优雅的方式,但我今天的努力没有产生任何实质性的成果。

I had a similar problem mounting a Rails 3.1 engine. I was receiving blank assets in stage and production.

I found a solution at http://jonswope.com/2010/07/25/rails-3-engines-plugins-and-static-assets/comment-page-1/#comment-87 and tweaked it to suit Rails 3.1 asset locations:

initializer "static assets" do |app|
  app.middleware.insert_before ::Rack::Lock, ::ActionDispatch::Static, "#{root}/app/assets"
end

I imagine there is a more elegant way but my effort today didn't yield anything substantial.

燃情 2024-10-11 08:08:25

我不太了解宝石的包装/制作方式。但是为什么不能将 gems 公共文件夹中的 images/js/css 内容复制到 apps 公共文件夹中呢?我刚刚这样做了,它对我有用。这不是已经完成的事情了吗?

I don't know much about the way, gems are packaged/made. But why can't you copy the images/js/css stuff of your gems public folder into the apps public folder? I just did that and it worked for me. Is that not the done thing?

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