Asset Paths Broken After Upgrade to Rails 4 - asset-pipeline

I have upgraded from 3.2 to 4. But assets are broken.
I have been using "assets/img/work/1.jpg" but I can't access to them in this way now. I can only access with digest like "assets/img/work/1-90041f6a6f670bd667cbfb47a50b27d2.jpg" what should i do? Only way is using image_tag?
Is using erb in CSS and JS files cause performance issues?

Append RAILS_ENV=production to rake assets:precompile will bring back the digest in CSS.

Unfortunately, the way that assets works has changed in Rails 4 so that sprockets-rails only generates digested assets. See Changes from Rails 3x for more details.
As commented in this answer, there is a rake task at https://github.com/rails/sprockets-rails/issues/49#issuecomment-20535134 that may be helpful if you need to generate static assets.
I can't comment on performance issues with ERB in CSS and JavaScript. The rails guide implies that using ERB in JS and CoffeeScript is the "Rails Way" of doing what you require. If you are using sass, you can use image-url instead of an ERB file.

Related

How to compress assets without Rails or Sprockets?

I'm writing a Sinatra app and I'm quite tired of Sprockets (because it's hard to configure and doesn't support some libs). I'm thinking of moving to Compass for stylesheet management but I haven't found anything similar to it when it comes to scripts. Now I can manually compile coffeescript into Javascript and concatenate the resulting files, but how do I compress them for less size? Thank you very much.
You may take a look at Gulp. Here is the good article about using Gulp with Rails (but it would work for Sinatra too).

Digest doesn't seem to work with SVG-images inside a Sass file

I have upgraded a rails app to version 4, but have some issues with Sass on Heroku. It seems like none of my included image in the Sass file gets the digest appended to the file.
Since rails 4 only generates a digest version of the assets, this fails. What can I do so the images in the Sass file on Heroku, also appends the digest to the CSS?
When referencing images within a sass file that are served up by the asset pipeline, you need to use the helpers provided by the sass-rails gem. From the Rails site:
image-url("rails.png") becomes url(/assets/rails.png)
image-path("rails.png") becomes "/assets/rails.png"

How can I completely disable CoffeeScript in a Rails 3.1 app?

At the moment when I generate a new controller, Rails also generates a .js.coffee file for the controller as well. As I don't use CoffeeScript I want Rails instead generate .js files for me.
Is it enough to comment out the coffee-rails gem to completely disable CofeeScript in a Rails 3.1 app?
Comment out gem "coffee-script" in your Gemfile
Use .js instead of .js.coffee for your javascript files
Not sure if this counts for Rails 3.1 but in 4 you should also set the javascript_engine to :js in application.rb to instruct generators to create .js files instead of .js.coffee.
config.generators do |g|
# .. other configuration ..
g.javascript_engine :js
end
Koen and Gaurav Gupta have good answers!
If you want to make these changes automatically for every new Rails project, you can use a template file.
In ~/rails-template.rb
# Don't install coffeescript
gsub_file 'Gemfile', /^gem \'coffee-rails\'/ do
"\# gem 'coffee-rails'"
end
# Mess with generators to get the behavior we expect around new files
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
<<-'RUBY'
config.generators do |g|
# Always use .js files, never .coffee
g.javascript_engine :js
end
RUBY
end
Then in ~/.railsrc
-m ~/.rails-template.rb
Now whenever you run rails new, the coffeescript gem will be commented out, and new controllers will use .js instead of .coffee.
Tested on Rails 5.0.4, but I believe it should work for earlier versions as well.
As an aside, Rails templates, and generators in general, are super powerful. I'm a teacher and my students will typically create 15 to 20 rails projects through the course, and providing them with a good template file with debugging gems, spec style testing, etc. is a huge timesaver. After they've made the changes once themselves, of course. If you're interested, my personal .rails-template.rb is on GitHub.
Note for Rails 4, or if you're using 'turbolinks', 'uglifier', or any other kind of gem that requires the server to interpret javascript, comment them out as well.
I had this problem, as I am using codekit to compile my coffeescript.
I got around it by renaming my 'assets/coffee' folder to 'assets/cafe', so rail wouldn't find it.
Edit: What does work (and the ONLY thing that works for me, the above answer does not work) is to add a separate folder 'App/Coffee', and setting it to be compiled into the assets/javascript folder. If it's in the assets directory, rails will find it no matter the name.

How to automatically compile scss into css with assets pipeline in Rails 3.1?

The new rails 3.1 asset pipeline confused me a lot. In rails 3.0.x, with the sass gem, my global css file got updated when I edit .scss files. But in rails 3.1, that doesn't work anymore. It seems like I have to run a rake task to update my css files whenever I modify the .scss files. I feel like I misunderstand something about the new asset pipeline, but I don't know what it is. Can someone provide a solution or explanation to this problem? Thank you.
There are two possible causes of this. I am assuming that you are in development mode, and that all the gems are loaded.
1. Config
In development mode files are compiled on-demand and cached until any changes are made. You have to get the new 3.1 config options set in the right files or this might not work as expected.
Check out the Rails guides section on upgrading.
2. Extensions
The other is getting the extensions in the right order. For scss that would be file.css.scss. This tells Sprockets to parse the files as scss first, and that the have a css extension. If you had .erb on the end - file.css.scss.erb - then erb is processed first, then scss.
Upgrading apps is a bit tricky because so many things have changed. The asset pipeline guide has lots of useful information and advice.
Sass / SCSS has this built in already so you don't have to do ERB pre-processing.
image-url("image.png")
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
For me this problem resolved very easy.
I simple deleted all precompiled *.css files from assets/stylesheets and remain all *.scss files. Rails worked fine with *.scss directly, withoutn precompile.

Migrating a rails app up to 3.1, having trouble getting sass to behave

I'm migrating a pre-Rails-3.1 app up to 3.1 (actually, someone else did part of the work of attempting to migrate to 3.0; I'd like to go ahead and get it up to 3.1 now).
When I create a new Rails 3.1 app with rails new, the skeleton app seems fine.
When I run rake test or thin start in the migrated app, though, I get:
/Users/dwhsix/.rvm/gems/ruby-1.9.2-p290#zmy/gems/sass-rails-3.1.0.rc.6/lib/sass/rails/template_handlers.rb:32:in `<class:SassTemplate>': undefined method `default_mime_type=' for Sass::Rails::SassTemplate:Class (NoMethodError)
I think I have things configured correctly. Gems are correct, application.rb has:
config.assets.enabled = true
config.generators.stylesheet_engine = :sass
Is there more that is needed somewhere?
Thanks...
May be please try adding one more at application.rb, since rails 3.1 use scss as default.
config.sass.preferred_syntax = :sass
and there is some mime related settings at initializers/mime_types.rb, so you may try to play with that too.
You could also switch to scss if you wanted to, by using the sass-convert command. I originally used sass syntax, but later decided I liked scss better and switched to that.
sass-convert -F sass -T scss mystyle.sass mystyle.scss
Okay, this turned out to be a problem of having too many versions of various gems installed. The real culprit was the wrong version of tilt, which was being brought in by some other gem. Clearing that, and resetting Gemfile.lock, took care of it.
I had the same problem just now. My solution was to update the tilt gem as described in this github issue. Apparently, the default mime type method, is added after tilt 1.3.0.

Resources