Skip to content

Loading…

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

Closed
awd-switzerland opened this Issue · 10 comments

5 participants

@awd-switzerland

This issue was first opened as a sass bug: sass/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

@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

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
Haml member

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

@chriseppstein

@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
Haml 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.

@chriseppstein

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
Haml 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.

@norman
Haml 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.

@norman
Haml 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.

@norman norman closed this in 022e102
@tiegz

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
Something went wrong with that request. Please try again.