代码之家  ›  专栏  ›  技术社区  ›  apneadiving

Rails 3引擎和静态资源

  •  7
  • apneadiving  · 技术社区  · 13 年前

    我正在建立一个引擎,我已经捆绑作为一个宝石(gmaps4rails)。我在rails应用程序的/public中复制了引擎的/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=真

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

    2. 我听从了建议 here 没有成功。

    6 回复  |  直到 11 年前
        1
  •  7
  •   Community Egal    7 年前

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

    看看这个 discussion 如果您使用nginx作为web服务器。

        2
  •  5
  •   Juanin    13 年前

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

    config.serve_static_assets=真

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

        3
  •  4
  •   rgoncalves    13 年前

    在发动机中,更换:

    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
  •  2
  •   Steve Lorek    13 年前

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

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

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

        5
  •  1
  •   Fudgie    12 年前

    我在安装Rails3.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
  •  0
  •   jmk    13 年前

    我不太了解宝石的包装方式。但是为什么不能将gems公共文件夹中的images/js/css文件复制到apps公共文件夹中呢?我就这么做了,这对我很有用。这不是做过的事吗?