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

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.

Related

Twitter Bootstrap 2 breaks Rails 3.1 page scripts?

In attempting to upgrade to Bootstrap 2 (using twitter-bootstrap-rails gem), it seems that page-specific scripts are rendered unusable: all scripts must be loaded from the Asset Pipeline to work. Even a simple $ alert function called from a page script tag doesn't appear to load, even if yielded into the head after the AP loads. The original Bootstrap played fine with page scripts.
I understand that scripts should usually be loaded through the AP as a practice, but has anyone else experienced this behavior, and is this something that can be remedied easily, or is this something unique to my setup? (Rails 3.1.3, rvm, ruby 1.9.3, xCode 4.1)
A git reset solved the issue, but I'd rather upgrade and use the new TB features....
Thanks for any insights or thoughts about how to track down the issue.
You are using rails 3.1.3 the new gem works fine for me on rails 3.2.2 so it should be that your jquery-rails gem might not be using the latest jquery. so try upgrading cos the new gem works with the latest version of jquery

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 do I change the default gemfile created with 'rails new' command?

I have recently experienced an issue where I must add the following to my gemfile:
gem 'execjs'
gem 'therubyracer'
I must do this to avoid a javascript runtime error that occurs when starting the rails server. I would like to have this modification added to all new gemfiles created with the rails new command.
You're looking for application templates.
Rails documentation on Application Templates
If you want the option to customize each app individually instead of having a rigid template, a really good option is Rails Composer. It prompts you about common gems during setup, and it nails a lot of the more common gems.
Finally, if you like Rails Composer, but want to be able to re-use application templates, check out Rails Apps Composer. I haven't looked into this too much, but it seems to give you a lot of flexibility while doing most of the heavy lifting for you.

getting error after ugrading to sass-3.1.8

after upgrading to sass-3.1.8 form sass-3.1.7 I get this error:
Functions may only be defined at the root of a document.
any Idea how I can solve this?
I'm using some of bourbon's mixins and it's imported at the top of my stylesheets, that's all.
I have the same problem and could not solve it by modifying code.
The way I solved was to use an older version:
gem uninstall sass
gem install sass -v 3.1.1
Ok Here is what I come up with:
SASS team decided to make a change (in this case "Functions may only be defined at the root of a document.") that made some plugins incompatible. in my case it was bourbon library. I made a ticket on github homepage of the bourbon and the owner updated the code and released a new version that's working with latest api.
I think this change should have got a bigger version bump to indicate the api change.
Sass developer here. Mixins and functions were never meant to be allowed in a scoped context. A bug was fixed recently that caused them to be caught when in an imported file (before this fix they were only caught if defined in the primary sass file).
That said, it's not a feature we're explicitly opposed to, but we'd would need to properly test it, document it, and support it as an official feature.

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.

Resources