I’m trying to deploy a Rails 3 app which includes jQuery Mobile. It works fine in development, and I understand that it needs to precompile the JS and CSS for production. I’m getting the following error:
Started GET "/orders/mobile" for 127.0.0.1 at Wed Jun 06 14:22:40 -0400 2012
Processing by OrdersController#mobile as HTML
Rendered orders/mobile.html.erb within layouts/mobile (642.9ms)
Completed 500 Internal Server Error in 1122ms
ActionView::Template::Error (jquery.mobile isn't precompiled):
5: <head>
6: <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7: <title>Company Orders</title>
8: <%= stylesheet_link_tag 'jquery.mobile' %>
9: <%= stylesheet_link_tag 'jquery.mobile.structure' %>
10: <%= stylesheet_link_tag 'jquery.mobile.theme' %>
11: <%= stylesheet_link_tag 'http://fonts.googleapis.com/css?family=Ubuntu' %>
app/views/layouts/mobile.html.erb:8:in `_app_views_layouts_mobile_html_erb__605278794_69818059003840'
app/controllers/orders_controller.rb:330:in `mobile'
app/controllers/orders_controller.rb:329:in `mobile'
I’ve read the usual stuff, but there are so many things wrong here, I hardly know where to start:
- I had the JQM stuff in app/assets, but have since moved them to vendor/assets. They get seen by the precompiler — I know because it will complain about them on various tries — but they never seem to get precompiled (in either location).
- I’ve tried
*= require_tree ../../../vendor/assets/stylesheetsin application.css. I don’t really want it included in every page hit, so I’d like to include it in the layout, but I’m just trying to get it precompiled somehow. - I’ve tried
*= require jquery.mobile[[.css].erb]in application.css. - I’ve tried
config.assets.precompile += %w( [[./]vendor/assets/stylesheets/]jquery.mobile.* )in config/environments/production.rb.
As a last-ditch effort, I’ve removed the “.erb” from jquery.mobile.css.erb, and removed the <% asset_data_uri %> tags to see if it would compile. It passes the rake assets:precompile command, but still gives me the same error.
I don’t want to turn off precompiling for JQM; I want it to work. (I really need to speed this page up.) However, I can’t find any guide on how to get JQM elegantly inserted into a Rails 3 app (with precompiling), and I’ve exhausted every avenue I can think of in trial and error. Surely someone has done this and knows the right way to go about it.
My fundamental problem here is that I need to have some assets precompiled for production, but NOT included in every page hit. Here’s what I’ve figured out:
If I didn’t mind JQM being served with every page, I could either put the files in app/assets and let
require_tree .do its magic, or leave those files in vendor/assets, andrequirethem individually in application.js. If I want to put them in vendor/assets and NOT specifically require them in application.js, I can useconfig.assets.paths << "#{Rails.root}/vendor/assets"in production.rb, and (apparently)require_treein application.js will process this tree as well. This still rolls them up into the compiled application JS “ball,” but leaves them distinct on the filesystem.The proper way to incorporate jQuery Mobile for specific pages — and keep its files separated in vendor/assets — seems to be to use both a
config.assets.paths << "#{Rails.root}/vendor/assets"directive and aconfig.assets.precompile += %w( jquery.mobile.* )directive. This will get the JQM files precompiled, but NOT rolled up into application-(hash).js and application-(hash).css. (They’ll be individual files in public/assets.) Then you can do specificjavascript_include_tag‘s andstylesheet_link_tag‘s in a layout specially for mobile views.Big finish: I had upgraded to Rails 3.2.5 because I saw some security report on 3.2.3. I rolled back to 3.2.3 and — incorporating what I learned above — I was able to finally get it working. I also tried 3.2.4. Apparently there’s a regression in > 3.2.3 that interferes with precompiling.