Jammit does not loads assets - ruby-on-rails

I'm developing the web site using Aptana 2.04.
When i say "script/server" from the project folder, everything is ok
After copying the project folder to another place, and saying "script/server", server starts, but jammit does't loads the packaged assets and i see the web page without any css and js files loaded.
I played a little with this problem, and found following:
When i say "jammit" from the non-copied folder, it packages the assets.
When i say "jammit" from the copied folder, i get an error "Jammit Warning: No assets match" for each .js and .css file
Whats wrong here ?
Help !
Update 1
I refer to each asset in "assets.yml" in follwing way:
javascripts:
common:
- my_styles.css
my_styles.css are in "public/assets" folder.
I also tried "- public/assets/my_styles.css", but it does't work
Update 2
All works in production and development modes, when i starting the server from my project folder.
But, when i just copy the project folder to some other place, i get the jammit errors. Two folders are the same byte by byte, but the behaviour is different. This is what confuses me ...

Doesn't look like you're referring to your assets properly -- Jammit will cache packaged assets into the public/assets folder -- the files shouldn't be in there to start with. Here's an example directory structure and assets.yml for you:
Directories:
public
javascripts
script1.js
script2.js
script3.js
assets.yml:
javascripts:
common:
- public/javascripts/*.js
Hope that helps you out. In development, you should see all scripts included as individual tag, and in production, you should see a single assets/common.js file.

Related

What is the difference between shared/public/assets on production server and pre-compiled assets on rails public/assets on source code?

I pre-compiled my assets i.e CSS,javascript and fonts files in order to reduce the file size. Using RAILS_ENV=production rake assets:precompile.
I already have compressed assets on the server in the following path:
shared/public/assets
But anyway I went ahead and pre-compiled them again on my local and they got generated inside public/assets folder. I noticed that the compressed files are exactly as same as the ones on server shared/public/assets. But the tester in my team is been testing it on some online tools. And they all say "Your java script files need to be minified." So, would this "minifying" issue be solved if I push these locally pre-compiled assets to production source code?
You should not push locally pre-compiled assets to production source code, no.
Your build process should include precompiling assets for production during a deploy. If you're using Rails 5 then that is already turned on by default. As long as the production environment has a proper environment variable (again, by default, the environment variable 'production' takes care of all these things), the assets will be precompiled.
You say that your compressed files, after running rake assets:precompile, are exactly the same as the ones on the server. That means your javascript should have and should be minified and uglified (again, default for Rails 5). To confirm that's the case, open dev tools in chrome, hard-reload (ctrl+shift+r) and check with JS files are being loaded under "network" tab. If the asset pipeline was used as it should be, you should only see minified and uglified js files here. If you open them up, they should already be minified and uglified. If that's not the case then either the asset pipeline wasn't used for fetching the JS files or your build process has been changed.
If most of these JS files are minified and uglified with - check those which aren't. Is some library being added outside of asset pipeline? Are those files minified?
If all of your JS files are actually minified, then tell your tester to use a different tool ;)

rails vendor folder angular2 development and production

Hello very amazing coders,
I'm currently using npm to manage angular2 dependencies. I then manually copy the required angular2 files from node_modules into my rails vendor folder. The rails will make those files available for me to load. However, I've read that vendor files should not be in source control. Ok, fair enough, I agree. However, if I .gitignore these files, they do not make it onto heroku. And subsequently my <script></script> tags in app/views/layouts/application.html.erb return 404's not found.
I'm looking for overall guidance on how you guys do vendor assets in production vs. development? In my case, a gem for angular2 source files does not exist, so pretend that is not an option.
There's so many flavors for this and as everything else it depends on your project. Here are some:
1- You can use cdn versions of the libraries in production, which is supposedly better because there's a good chance that people visiting your page would have already downloaded jquery or w/e from a cdn while visiting another page so you they don't have to download yet another copy for your page alone. Like https://www.maxcdn.com/ or https://developers.google.com/speed/libraries/.
2- You can have one repo with your code on it, sources and all, without the vendor files and one more repo connected to heroku where you push only the distributable build of your project including if you want, the vendor files.
3- Run the same npm install and all other configuration scripts you normally use from your production server, Heroku should allow you to do this np, or at least have a plugin for it.
I hope this helps.

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.
this is because things work differently in development as compared to production.
few thing to note:-
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.
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
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
Run RAILS_ENV=production rake assets:clean assets:precompile
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.

Jekyll blog in Rails site using Bloggy gem: how do I use the Rails css for the blog, on each build?

I've got a Rails site with a Jekyll blog incorporated, using the Bloggy gem.
I'd like a similar look for the main site and the blog, so I want to use the css in app/assets/stylesheets, but those files are in css.scss format. Jekyll (in a Bloggy setup) looks for css in config/jekyll/css, and seems to only want .css files; symlinking the Rails css directory into the Jekyll hierarchy doesn't seem to to work.
Is there a way to take advantage of the asset pipeline so that when I run jekyll:build, SCSS files from the Rails app are made into CSS files, placed in the appropriate jekyll directory, and bundled with the latest Jekyll build as it's placed into the /public/blog folder?
Thanks!
Ended up getting through this by:
Using the jekyll-sass gem to allow automatic transformation of the Rails app's .css.scss into .css.css files. By symlinking the Rails app/assets/stylesheets directory into Bloggy's config/jekyll/css, this put files with the right content but wrong extensions in the correct place.
Writing a rake task to make the .css.css files into .css files.
desc 'Make .css.css files into .css files'
task :css_css do
Dir.glob('public/blog/css/*.css.css').each do |file|
puts `mv #{file} #{file.gsub(/\.css\.css$/, '.css')}`
end
end
Not the prettiest solution, but it works.
#Matthew.. You have a nice solution.. For this part I did some stuffs manually. Like I added config/jekyll/css files as .css extension rather than .css.scss so when we run "rake generate" for bloggy the right format files are created on the folder public/blog/ rather css.scss.
I have made the changes in my bloggy portfolio theme project repository. If you are planning to use my version of code, I have did some changes like added robots.txt, sitemap, and integrated bootstrap to the project. I have also removed all database connections from the rails project since it was showing errors while deploying into heroku.

Where should uploaded files get stored in Rails 3.1?

When user uploads files. In Rails 3.0+, these would go into public/uploads. In 3.1, should uploaded files go to app/assets/uploads? Or still in public/uploads?
It's not really an issue in our environment, since we are using S3. Just trying to understand Rails 3.1's new directory structure.
What are your thoughts?
the public directory, capistrano recommends public/system/
don't get confused by the app/assets directory, it's usually for css/js/coffeescript files, think this is the biggest change from 3.0 to 3.1
Well, the answer is simple: your users will only have access to your /public directory.
There are just some tricks to get css and js but you'll have to stick with /public for the other stuff.
Generally, I put all stuff in /public/assets
adding on to apneadiving's answer:
if you use Carrierwave , the temporary files are in your system's /tmp directory and the uploaded files are in a subdirectory underneath $RAILS_ROOT/public , e.g. $RAILS_ROOT/public/uploads/YOUR-MODEL/...
In Rails 3.1 the 'assets' directory is meant for the JavaScript and CSS files so that sprockets can pick them up there and so that they are not accessible directly via the "public" directory...
see: assets/javascripts/application.js and assets/stylesheets/application.css files
see: http://railscasts.com/episodes/265-rails-3-1-overview
The app/assets directory is for CoffeeScript files (also not publicly accessible, so not a place to put uploads)
Putting uploaded files in the filesystem only works if you have one file server or a network mapped storage... I usually just put the files in the database itself.
But as vrsmn said, don't use assets for this, assets pipeline is for streamlining the css/js/application images.

Resources