unom
unom

Reputation: 11476

Rails app assets view just fine in WEBrick but no CSS, Javascript or Images from apache2?

I have a rails app configured on my Raspberry Pi 2. It's on my local LAN getting the same ip over and over from the router. It has mDNS configured so I can access it at mypi.local.

Configured apache2 and passenger for that local domain. Now when I enter mypi.local in my browser I see the app. Great!

It works, but for some unknown reason I get all the app's html but without any CSS and Javascript. I can interact with parts of the site which are not Javascript dependent and my browser's default CSS kicks in.

Any ideas?

If I launch WEBrick and try mypi.local:3000 everything works as expected.

Upvotes: 0

Views: 382

Answers (1)

Milind
Milind

Reputation: 5112

this is because things work differently in development as compared to production. few thing to note:-

  1. No CSS or JS files will be available to your app through the asset pipeline unless they are included in other files OR listed in the config.precompile directive.Only application.css and application.js are available by default of all the CSS and JS files.
  2. Every file that is not a Javascript file or CSS file that is in the app/assets folder will be copied by Rails into the public/assets folder when you compile your assets.So if you want to add some web fonts, you could make an app/assets/fonts/ folder and put your fonts in there, these will then be copied to public/assets/fonts folder when you compile your assets. Note that your app/assets/stylesheets/fonts.css.scss file that references those fonts will NOT be copied over unless you either added it to the config.assets.precompile directive or required it from your application.css
  3. for config.assets.compile...If it is set to "true" (which it is by default in development) then Rails will try to find a Javascript or CSS file by first looking in the public/assets directory and if it can't find it, will hunt through your app/assets folder looking for the file. If it finds it in app/assets it will go ahead and compile on the fly and then serve this asset up.

The problem with this is that you don't notice it happening in development, then you commit everything and push to production and BOOM, everything is broken with 500 errors because production has config.assets.compile set to "false".This prevents the app from "falling back" and trying to load the file directly instead of using the asset pipeline.

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

Why don't you just have this set to "true" in every environment? Well, because it is sloooooow. And you don't want slow in production

  1. Run RAILS_ENV=production rake assets:clean assets:precompile
  2. check public/assets directory and verify that assets are compiled..if its not empty...that means asset pipeline is working but path is not correct.use asset_helpers to set the path of assets in css files.

Upvotes: 1

Related Questions