Rails 3 Engine & Static assets

懵懂的女人 提交于 2019-11-30 13:05:53

问题


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.


回答1:


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.




回答2:


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)




回答3:


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



回答4:


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.




回答5:


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.




回答6:


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?



来源:https://stackoverflow.com/questions/4266232/rails-3-engine-static-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!