【问题标题】:Rails 3 Engine & Static assetsRails 3 引擎和静态资源
【发布时间】:2010-11-24 11:27:21
【问题描述】:

我正在构建一个捆绑为 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. 我按照 here 的建议没有成功。

【问题讨论】:

    标签: ruby-on-rails-3 static assets rails-engines


    【解决方案1】:

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

    如果您使用 nginx 作为网络服务器,请查看 discussion

    【讨论】:

      【解决方案2】:

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

      config.serve_static_assets = true

      默认情况下,Rails 假定您使用的是资产服务器(lightttp、nginx 或 Apache)

      【讨论】:

        【解决方案3】:

        在您的引擎中,替换:

        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
        

        【讨论】:

          【解决方案4】:

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

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

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

          【讨论】:

          • 感谢您的回答,我确实尝试过,但没有任何区别:)
          【解决方案5】:

          我在安装 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
          

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

            • 这意味着使用您的 gem 的每个人都必须手动将公共文件复制到他们的主应用程序中......不,这还没有完成!
            猜你喜欢
            • 2019-03-15
            • 2014-08-29
            • 2023-03-03
            • 1970-01-01
            • 2014-07-29
            • 1970-01-01
            • 1970-01-01
            • 2016-06-27
            • 2023-04-01
            相关资源
            最近更新 更多