image-url helper raises an exception when used inline #467

Closed
awd-switzerland opened this Issue Dec 5, 2011 · 10 comments

Comments

Projects
None yet
5 participants
@awd-switzerland

This issue was first opened as a sass bug: nex3/sass#201
Upon nex3 stating this was a sass-rails bug instead, it was moved on to sass-rails: rails/sass-rails#68
Where chriseppstein claimed this was a haml bug. So here we are…

The following snipped in _some_partial.haml fails reproducably with sass 3.1.10, rails 3.1.1 and ruby 1.9.2p290:

%style(type="text/css")
  :sass
    #whatever
      background-image:   image-url('some/asset.png')

If you put the exact same piece of sass into an external sass file, no exception is raised.

The exception we get is:

NoMethodError
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Backtrace (up to the entry point from actionpack, we can provide the rest too if necessary):

sass-rails (3.1.4) lib/sass/rails/helpers.rb:40:in `resolver'
sass-rails (3.1.4) lib/sass/rails/helpers.rb:25:in `image_url'
sass (3.1.10) lib/sass/script/funcall.rb:88:in `_perform'
sass (3.1.10) lib/sass/script/node.rb:40:in `perform'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:214:in `visit_prop'
sass (3.1.10) lib/sass/tree/visitors/base.rb:37:in `visit'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:18:in `visit'
sass (3.1.10) lib/sass/tree/visitors/base.rb:53:in `block in visit_children'
sass (3.1.10) lib/sass/tree/visitors/base.rb:53:in `map'
sass (3.1.10) lib/sass/tree/visitors/base.rb:53:in `visit_children'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:27:in `block in visit_children'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:39:in `with_environment'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:26:in `visit_children'
sass (3.1.10) lib/sass/tree/visitors/base.rb:37:in `block in visit'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:234:in `visit_rule'
sass (3.1.10) lib/sass/tree/visitors/base.rb:37:in `visit'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:18:in `visit'
sass (3.1.10) lib/sass/tree/visitors/base.rb:53:in `block in visit_children'
sass (3.1.10) lib/sass/tree/visitors/base.rb:53:in `map'
sass (3.1.10) lib/sass/tree/visitors/base.rb:53:in `visit_children'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:27:in `block in visit_children'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:39:in `with_environment'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:26:in `visit_children'
sass (3.1.10) lib/sass/tree/visitors/base.rb:37:in `block in visit'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:47:in `visit_root'
sass (3.1.10) lib/sass/tree/visitors/base.rb:37:in `visit'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:18:in `visit'
sass (3.1.10) lib/sass/tree/visitors/perform.rb:7:in `visit'
sass (3.1.10) lib/sass/tree/root_node.rb:20:in `render'
sass (3.1.10) lib/sass/engine.rb:300:in `_render'
sass (3.1.10) lib/sass/engine.rb:248:in `block in render'
sass (3.1.10) lib/sass/util.rb:297:in `silence_sass_warnings'
sass (3.1.10) lib/sass/engine.rb:248:in `render'
haml (3.1.3) lib/haml/filters.rb:311:in `render'
haml (3.1.3) lib/haml/filters.rb:71:in `render_with_options'
haml (3.1.3) lib/haml/filters.rb:121:in `block in compile'
haml (3.1.3) lib/haml/filters.rb:99:in `instance_eval'
haml (3.1.3) lib/haml/filters.rb:99:in `compile'
haml (3.1.3) lib/haml/filters.rb:80:in `internal_compile'
haml (3.1.3) lib/haml/compiler.rb:210:in `compile_filter'
haml (3.1.3) lib/haml/compiler.rb:447:in `compile'
haml (3.1.3) lib/haml/compiler.rb:446:in `block (2 levels) in compile'
haml (3.1.3) lib/haml/compiler.rb:446:in `each'
haml (3.1.3) lib/haml/compiler.rb:446:in `block in compile'
haml (3.1.3) lib/haml/compiler.rb:170:in `compile_tag'
haml (3.1.3) lib/haml/compiler.rb:447:in `compile'
haml (3.1.3) lib/haml/compiler.rb:446:in `block (2 levels) in compile'
haml (3.1.3) lib/haml/compiler.rb:446:in `each'
haml (3.1.3) lib/haml/compiler.rb:446:in `block in compile'
haml (3.1.3) lib/haml/compiler.rb:48:in `compile_root'
haml (3.1.3) lib/haml/compiler.rb:447:in `compile'
haml (3.1.3) lib/haml/engine.rb:124:in `initialize'
haml (3.1.3) lib/haml/template/plugin.rb:34:in `new'
haml (3.1.3) lib/haml/template/plugin.rb:34:in `compile'
haml (3.1.3) lib/haml/template/plugin.rb:39:in `call'
actionpack (3.1.1) lib/action_view/template.rb:253:in `compile'

Thanks for having a look at it.

For the fellow reader who has the same issue, joevandyk suggested a workaround:

background-url: '#{ image_path('email/logo-high-res.png') }'
@chriseppstein

This comment has been minimized.

Show comment
Hide comment
@chriseppstein

chriseppstein Dec 5, 2011

Contributor

@nex3 I'm happy to work with you to figure out how to connect Haml & sass-rails. The main concerns that needs to be considered is this: The asset pipeline is usually disabled in production which means that Sass::Rails won't even be required. As a result, the asset helpers defined there are not available which makes referencing assets like images basically impossible. Haml will need to either reproduce these helpers (or require them somehow) or direct users to use #{ } in a sass filter so they can use the normal view helpers instead.

Alternatively, Sass::Rails could provide sass filters for haml users if Haml is around (or override the existing sass filter), but this would basically force users to enable the asset pipeline. Anyways. it's a bit of a clusterf*ck. Let's chat.

Contributor

chriseppstein commented Dec 5, 2011

@nex3 I'm happy to work with you to figure out how to connect Haml & sass-rails. The main concerns that needs to be considered is this: The asset pipeline is usually disabled in production which means that Sass::Rails won't even be required. As a result, the asset helpers defined there are not available which makes referencing assets like images basically impossible. Haml will need to either reproduce these helpers (or require them somehow) or direct users to use #{ } in a sass filter so they can use the normal view helpers instead.

Alternatively, Sass::Rails could provide sass filters for haml users if Haml is around (or override the existing sass filter), but this would basically force users to enable the asset pipeline. Anyways. it's a bit of a clusterf*ck. Let's chat.

@bsjaekel

This comment has been minimized.

Show comment
Hide comment
@bsjaekel

bsjaekel Dec 11, 2011

I get the same error, when using image-url in a scss file which gets imported into my main scss file via @import. So i am a littlebit unsure if this a haml problem, as haml is not involved in this process.

I get the same error, when using image-url in a scss file which gets imported into my main scss file via @import. So i am a littlebit unsure if this a haml problem, as haml is not involved in this process.

@norman

This comment has been minimized.

Show comment
Hide comment
@norman

norman Jun 15, 2012

Member

Is this still an issue? Happy to look into it if need be.

Member

norman commented Jun 15, 2012

Is this still an issue? Happy to look into it if need be.

@chriseppstein

This comment has been minimized.

Show comment
Hide comment
@chriseppstein

chriseppstein Jun 19, 2012

Contributor

@norman as far as I know, it still exists. should be easy to verify. Please let me know what support you need from Sass::Rails.

Contributor

chriseppstein commented Jun 19, 2012

@norman as far as I know, it still exists. should be easy to verify. Please let me know what support you need from Sass::Rails.

@norman

This comment has been minimized.

Show comment
Hide comment
@norman

norman Jun 20, 2012

Member

@chriseppstein I'm tempted to leave this at documenting it as a known deficiency. I don't think it's a good idea for either Sass::Rails or Haml to include or reimplement portions of the asset pipeline, especially when there are reasonably straightforward alternatives like putting the Sass code in a .sass file or using interpolation.

Member

norman commented Jun 20, 2012

@chriseppstein I'm tempted to leave this at documenting it as a known deficiency. I don't think it's a good idea for either Sass::Rails or Haml to include or reimplement portions of the asset pipeline, especially when there are reasonably straightforward alternatives like putting the Sass code in a .sass file or using interpolation.

@chriseppstein

This comment has been minimized.

Show comment
Hide comment
@chriseppstein

chriseppstein Jun 20, 2012

Contributor

It's your call, of course, but I think if Haml is to provide a :sass filter in Rails, then it should work as sass works elsewhere in the application. The asset url helpers are available elsewhere in the rails template, so this should be doable and it would be consistent with the behavior at the markup level.

Contributor

chriseppstein commented Jun 20, 2012

It's your call, of course, but I think if Haml is to provide a :sass filter in Rails, then it should work as sass works elsewhere in the application. The asset url helpers are available elsewhere in the rails template, so this should be doable and it would be consistent with the behavior at the markup level.

@norman

This comment has been minimized.

Show comment
Hide comment
@norman

norman Jun 27, 2012

Member

@chriseppstein ok. I'm going to tag 3.2 and prepare for a beta release today, but I've added this issue to the 3.2 milestone to come up with a resolution before the full release.

Member

norman commented Jun 27, 2012

@chriseppstein ok. I'm going to tag 3.2 and prepare for a beta release today, but I've added this issue to the 3.2 milestone to come up with a resolution before the full release.

@norman

This comment has been minimized.

Show comment
Hide comment
@norman

norman Jan 25, 2013

Member

Ok, after about 2 hours of slogging through Sprockets/Sass::Rails anonymous class hell, I've come up with some very rough code that enables asset helpers inside filters for Rails. It still needs quite a bit of work, but it looks like solving it on the Haml side is the right approach.

Member

norman commented Jan 25, 2013

Ok, after about 2 hours of slogging through Sprockets/Sass::Rails anonymous class hell, I've come up with some very rough code that enables asset helpers inside filters for Rails. It still needs quite a bit of work, but it looks like solving it on the Haml side is the right approach.

@norman

This comment has been minimized.

Show comment
Hide comment
@norman

norman Jan 25, 2013

Member

Oh, and if you're playing along at home, keep in mind you still need to pull sass-rails out of the assets group in your Gemfile and put it in with the other gems that get loaded in production.

Member

norman commented Jan 25, 2013

Oh, and if you're playing along at home, keep in mind you still need to pull sass-rails out of the assets group in your Gemfile and put it in with the other gems that get loaded in production.

@norman norman closed this in 022e102 Jan 28, 2013

@tiegz

This comment has been minimized.

Show comment
Hide comment
@tiegz

tiegz Sep 25, 2013

Contributor

Has anyone else run into this error using inline Sass with HAML 3.2.0.rc.3 or above?

TypeError: no implicit conversion of nil into String
Sass::Rails::Importer#resolve  sass-rails (3.2.6) lib/sass/rails/importer.rb, line 36
Sass::Rails::Importer#find_relative  sass-rails (3.2.6) lib/sass/rails/importer.rb, line 47
Sass::Tree::ImportNode#import  sass (3.2.10) lib/sass/tree/import_node.rb, line 49
Sass::Tree::ImportNode#imported_file  

afaict, it's because the Context#pathname is passed in via the SassRailsTemplate as / here, causing Sprockets' Context#root_path to return nil.

Contributor

tiegz commented Sep 25, 2013

Has anyone else run into this error using inline Sass with HAML 3.2.0.rc.3 or above?

TypeError: no implicit conversion of nil into String
Sass::Rails::Importer#resolve  sass-rails (3.2.6) lib/sass/rails/importer.rb, line 36
Sass::Rails::Importer#find_relative  sass-rails (3.2.6) lib/sass/rails/importer.rb, line 47
Sass::Tree::ImportNode#import  sass (3.2.10) lib/sass/tree/import_node.rb, line 49
Sass::Tree::ImportNode#imported_file  

afaict, it's because the Context#pathname is passed in via the SassRailsTemplate as / here, causing Sprockets' Context#root_path to return nil.

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