Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

undefined method `asset_environment' for class `Sprockets::Railtie' (NameError) #22

chriseppstein opened this Issue Jul 27, 2011 · 59 comments


None yet

chriseppstein commented Jul 27, 2011

Stack trace:

/Users/chris/Projects/rails/activesupport/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method': undefined method `asset_environment' for class `Sprockets::Railtie' (NameError)
    from /Users/chris/Projects/rails/activesupport/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
    from ./lib/sass/rails/monkey_patches.rb:7:in `included'
    from ./lib/sass/rails/monkey_patches.rb:23:in `include'
    from ./lib/sass/rails/monkey_patches.rb:23
    from ./lib/sass/rails.rb:9:in `require'
    from ./lib/sass/rails.rb:9
    from ./test/test_helper.rb:6:in `require'
    from ./test/test_helper.rb:6
    from /Users/chris/Projects/sass-rails/test/sass_rails_logger_test.rb:1:in `require'
    from /Users/chris/Projects/sass-rails/test/sass_rails_logger_test.rb:1
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9:in `require'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9:in `each'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5:in `each'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5

@ghost ghost assigned chriseppstein Jul 27, 2011

rubys commented Jul 27, 2011

Reproduction instructions (with latest rails 3-1-stable):

rails generate controller Say hello goodbye

Full scenario and captured output: http://intertwingly.net/projects/AWDwR4/checkdepot-192-31/section-2.html

rubys commented Jul 27, 2011

git bisect output:

1edfadf82e992c98de53eaef4bde02fa4f47ec5e is the first bad commit
commit 1edfadf82e992c98de53eaef4bde02fa4f47ec5e
Date:   Wed Jul 27 15:09:42 2011 -0500

    Make Rails.application.assets available in initializers

Note: markdown is being too helpful here. Correct link for the commit is as follows:



josh commented Jul 27, 2011

Should be able to move that monkey patch into the railtie initializer:

initializer :setup_sass do |app|
  # ...
  app.assets.context_class.sass_config = app.config.sass

ghost commented Jul 28, 2011

same issue here with rails-3-1-stable & sass-rails-3.1.0.rc.5

rubys commented Jul 28, 2011

I can verify that the following works on rails master:

module Sass::Rails::SassContext
  attr_accessor :sass_config

  # Before sprockets was extracted from rails
  require 'sprockets/railtie'
  module Sprockets
    class Railtie < ::Rails::Railtie
      initializer :setup_sass do |app|
        app.assets.context_class.sass_config = app.config.sass
rescue LoadError
  # After sprockets was extracted into sprockets-rails
  require 'sprockets/rails/railtie'
  module Sprockets
    module Rails
      class Railtie < ::Rails::Railtie
        include Sass::Rails::SprocketsConfig

Note that I only added the code that Josh provided to the Before sprockets was extracted from rails code path as that is the path used with the Rails master.

rubys commented Jul 28, 2011

I've now verified that the same code works with 3-1-stable. As sass-rails versions are tightly coupled with Rails versions, I suggest that the rescue LoadError is not needed?


josh commented Jul 28, 2011

sass-rails can add a requirement for rails ~> 3.1.0.rc5 and remove any old hacks.


chriseppstein commented Jul 28, 2011

Some new commits on https://github.com/rails/sass-rails/commits/3-1-stable please let me know if there's any other issue so I can release.


grk commented Jul 28, 2011

Fixed the one in the issue, but created another one:

undefined method `context_class' for nil:NilClass
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/activesupport/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/sass-rails-c0ff27f44c4a/lib/sass/rails/railtie.rb:40:in `block in <class:Railtie>'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/initializable.rb:25:in `instance_exec'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/initializable.rb:25:in `run'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/initializable.rb:50:in `block in run_initializers'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/initializable.rb:49:in `each'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/initializable.rb:49:in `run_initializers'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/application.rb:92:in `initialize!'
/Users/gk/.rvm/gems/ruby-1.9.2-p180-patched/bundler/gems/rails-ac24a53ffc02/railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
/Users/gk/Work/Projects/gastro/config/environment.rb:8:in `<top (required)>'

chriseppstein commented Jul 28, 2011

grrr. can someone see if they can fix this? I'm at work now.


grk commented Jul 28, 2011

Wow, github really surprised me with referencing this.


spastorino commented Jul 28, 2011

@grk can you provide a failing test case with it?


grk commented Jul 28, 2011

8 specs fail without it.

rubys commented Jul 28, 2011

I've tested @chriseppstein changes on both 3-1-stable and master, and they work for me.

I've tried @grk's version, and oddly my test is failing once I turn on:

config.action_controller.perform_caching = true

I'm going to try further tests to verify that this was caused by grk's changes and is not unrelated.


josh commented Jul 28, 2011

That fix doesn't do anything. Its as good as commenting out that code.


spastorino commented Jul 28, 2011

@grk commit is wrong, @rubys can you test it now? I've reverted it


josh commented Jul 28, 2011

assuming this fixes the problem, can you add it to the 3-1-stable branch?


ghost commented Jul 28, 2011

are you testing in both production and development modes? My assumption is yes but my trivial app runs in production but not in development with this mornings code.

johnnym commented Jul 29, 2011

I'm still getting undefined method 'context_class' like @grk got previously. Using rails 3-1-stable and sass-rails 3-1-stable.

/Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/activesupport/lib/active_support/whiny_nil.rb:48:in `method_missing': undefined method `context_class' for nil:NilClass (NoMethodError)
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/sass-rails-ad73d00ea9f2/lib/sass/rails/railtie.rb:40:in `block in <class:Railtie>'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/initializable.rb:25:in `run'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/initializable.rb:49:in `each'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/application.rb:92:in `initialize!'
    from /Users/johnny/.rvm/gems/ruby-1.9.2-p290/bundler/gems/rails-f2ade6f4b0d3/railties/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/johnny/test/config/environment.rb:5:in `<top (required)>'

@josh's commit fixes it but it's not part of the 3-1-stable branch yet...

johnnym commented Jul 29, 2011

Ah, yes. I somehow kept thinking that 3-1-stable was the working branch. But you're right, that require fixes the problem.


spastorino commented Jul 29, 2011

Pushed to 3-1-stable

huerlisi commented Aug 2, 2011

I'm still getting undefined method 'context_class' errors when running the tests in the 3-1-stable branch. This is with all the gems as per Gemfile.lock, running bundle exec rake test.


spastorino commented Aug 3, 2011

@huerlisi steps to reproduce it? did you run bundle update before? just checking because shouldn't be an issue anymore.

huerlisi commented Aug 3, 2011

@spastorino did not run bundle update but bundle which should use the gems per Gemfile.lock AFAIK. Will try with bundle update right now.

This is with ruby 1.8.7

huerlisi commented Aug 3, 2011

Same after a bundle update. Gemfile.lock was not modified, looks like using same gem/git versions as the one who checked in.

See log of commands and output at https://gist.github.com/1122050


spastorino commented Aug 3, 2011

@huerlisi It's working perfect for me but the way I'm able to test sass-rails it's odd.

I have cloned sprockets and sass and done this https://gist.github.com/93866fc6403d40d67d0d

Results of the tests for me ...

➜  sass-rails git:(3-1-stable) ✗ bundle exec rake test
Loaded suite /Users/santiago/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-0.9.2/lib/rake/rake_test_loader
Finished in 55.896585 seconds.

14 tests, 34 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 63056

Can you try doing the same?

/cc @chriseppstein it's there a better way to test sass-rails? just cloning sass-rails and running the tests I get the same results @huerlisi is getting

huerlisi commented Aug 4, 2011

@spastorino What rails/sprocket branches have you checked out?

AFAIK the sass-rails 3-1-stable branch should work with rails 3.1. To actually test it against this version you need to have rails branch 3-1-stable or similar checked out.

why is this ticket marked as "closed" ?
I just ran into this issue by rebuilding my Gemfile.lock, thus, updating the branch heads of my 3.1 rails.


spastorino commented Aug 4, 2011

@huerlisi use 3-1-stable branch of Rails, and master of sprockets and sass


spastorino commented Aug 4, 2011

@trapni have you tested with the steps I've provided?, can you provide info on what's wrong?

Same issue here. "undefined method asset_environment for class Sprockets::Railtie'
I'm using:

gem 'rails', '3.1.0.rc5', :git => "git://github.com/rails/rails", :branch => "3-1-stable"
gem "sass-rails"

Adding 3-1stable branch to sass-rails fixes the error:

gem "sass-rails", git: 'https://github.com/rails/sass-rails.git', branch: '3-1-stable'

But now, I'm getting the same issue than @johnnym:

"`method_missing': undefined method `context_class' for nil:NilClass"

@mariozaizar, the gem setup below is working for me (aside from a new issue in compass/blueprint).

gem "rails", :git => 'https://github.com/rails/rails.git', :branch => '3-1-stable'

gem 'sass', '3.1.5'
gem 'compass', :git => 'https://github.com/chriseppstein/compass.git', :branch => 'rails31'

group :assets do
  gem 'sass-rails', :git => 'https://github.com/rails/sass-rails.git', :branch => '3-1-stable'
  # others removed for simplicity

I forgot to mention... you should also gem uninstall sass -v3.1.6

@johnlauck, Thanks but that didn't work.. But, now I can confirm that doing the monkey patch from:

 # https://github.com/rails/sass-rails/commit/8cf2d6072727c9e70ee54c767c9055e23e1594f5
 app.config.assets.context_class.sass_config = app.config.sass

Totally works for "method_missing': undefined method context_class for nil:NilClass"
buuuut... Now I'm getting this: undefined method sass_config for #<Class:0x00000102028868>

I've fixed "method_missing': undefined method context_class' for nil:NilClass" by enabling asset pipeline (config.assets.enabled = true) in my application.rb.

Otherwise Sprockets::Railtie bails out too early failing to set-up app.assets (see https://github.com/rails/rails/blob/83d9398ec049e414ce57/actionpack/lib/sprockets/railtie.rb#L16) which leads to Sass::Rails::Railite blowing up on app.assets being nil here: https://github.com/rails/sass-rails/blob/890273d7eebd2a1c6b28/lib/sass/rails/railtie.rb#L42.

thedarkone added a commit to thedarkone/sass-rails that referenced this issue Aug 11, 2011


chriseppstein commented Aug 11, 2011

@thedarkone, why is it that you've got sass-rails install but the asset-pipeline disabled... This seems like a strange use case to me.

I'm trying to upgrade sass and compass (to their current masters) in my 3.1 app, I'm not comfortable with asset-pipeline yet and I have compass's Railtie blowing up because app.config.sass is no set up. I thought that was what sass-rails was for and then run into this weird error...

I see the same issue as @mariozaizar

In my case, it is caused by line 47 in template_handlers.rb, in the sass_options_from_rails method i.e.


I do not understand enough about Rails internals to know why the context_class does not have a sass_config method since the one used in railtie.rb does.

It would appear that the context_class in railtie.rb can not the same one used in this method.

Hi guys,
This is the current Gemfile that I'm using, and seems to be working now (I'm not using the monkey patch anymore):

gem 'rails', '3.1.0.rc5', :git => "git://github.com/rails/rails", :branch => "3-1-stable"
gem "sass-rails", :git => 'https://github.com/rails/sass-rails.git', :branch => '3-1-stable'
gem 'compass', :git => 'https://github.com/chriseppstein/compass.git', :branch => 'rails31'
gem 'haml', :git => "git://github.com/cheald/haml.git"
gem "haml-rails"



Thanks for posting that.

I changes my Gemfile from

gem 'rails', '3.1.0.rc5'

to use rc5 from github exactly as you do.

And the problem went away.

Which suggests there must be some difference between the "regular" gem and github?

ncri commented Aug 15, 2011

How can I get this working with gem 'rails', '3.1.0.rc5'?

I can't use gem 'rails', '3.1.0.rc5', :git => "git://github.com/rails/rails", :branch => "3-1-stable" on heroku, it just bloats my slug size far too much.

I also get "undefined method `context_class' for nil:NilClass (NoMethodError)"

@ncri do you have asset-pipeline enabled as per my comment above?

ncri commented Aug 15, 2011

Okay, as a first solution I removed the sass-rails gem - don't need it yet. ;-) Now it works.

ncri commented Aug 15, 2011

My asset pipeline is enabled, yes.

I have the same problem, and the only way to fix it is the same as @ncri: Remove the sass-rails gem :(

I bumped to sass-rails from git and seems to work fine now.

It turned out that I forgot to install Node.js and Coffescript (how stupid!) on the server. Installing them, and then reinstalling the last version of sass-rails, solved the problem. YAY!

ncri commented Aug 19, 2011

Yep, all works for me too now with rails rc6. I was able to add the sass-rails gem again.


chriseppstein commented Aug 22, 2011

fixed by 74c5e4e.

Sass rails will raise an error if the asset pipeline is not enabled -- in these cases the sass-rails plugin should not be used/activated.

I am getting the same error mentioned many times in above discussions
"undefined method `context_class' for nil:NilClass"
when trying to do rake db:migrate
Please help.

ncri commented Aug 24, 2011

@PrajktaP do you use rails 3.1 rc6?

No, its rails 3.1 rc4

ncri commented Aug 24, 2011

try upgrading

I have upgraded to rc6. Now I am getting following error
Please install the sqlite3 adapter: gem install activerecord-sqlite3-adapter (sqlite3 is not part of the bundle. Add it to Gemfile.)

ncri commented Aug 24, 2011

Yes, then just do it. :) I'm surprised though you didnt get that error with rc4.

Thanks for all you responses.
But I am not able to proceed further because of some or the other error :(

What do I do with this?

rake aborted!
uninitialized constant Encoding

Has it to do anything with my ruby version? I am using 1.8.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment