jamesmartin/inline_svg

NoMethodError - undefined method `inline_svg`

JonaMX opened this issue · 4 comments

I have been using the helper inline_svg on my erb views with no issues, I was using the version 0.6.1, I tried to upgrade the gem to the latest version which is 1.3.1 and now I'm having this issue.

NoMethodError - undefined method `inline_svg' for #<#<Class:0x00007ffbdd181f88>:0x00007ffbdcc0bec8>:
  app/assets/templates/authors-hub/assignment-work-item.html.erb:46:in `block in singleton class'
  app/assets/templates/authors-hub/assignment-work-item.html.erb:65530:in `singleton class'
  app/assets/templates/authors-hub/assignment-work-item.html.erb:65527:in `__tilt_70359753711160'
  app/views/home/index.html.erb:1:in `block in _app_views_home_index_html_erb___3952282048746299132_70359767600320'
  app/views/home/index.html.erb:1:in `_app_views_home_index_html_erb___3952282048746299132_70359767600320'
  app/controllers/home_controller.rb:7:in `index'

I'm using Rails 4.2.10

@JonaMX thanks for opening this issue. Sorry you're having trouble with inline_svg.

I tried to reproduce the error using my inline_svg Test Rails App, which I upgraded to use inline_svg v1.3.1 and Rails 4.2.10 but everything seems to be working fine: SVGs are embedded into the view templates.

Do you have any custom configuration for the gem (/config/initializers/inline_svg.rb), if so would you mind pasting the contents of that file in this issue?

Could you also paste the code for your view template, where you call the inline_svg method?

Hey @jamesmartin thanks for your quick response.

I just realized that the issue is within the templates/views that I'm serving with angular-rails-templates. It seems that the Helpers are not being loaded.

Answering your questions, no I don't have any custom configuration, the only thing that I've added regarding InlineSvg is this configuration for the assets

Rails.application.config.assets.context_class.class_eval do
  include InlineSvg::ActionView::Helpers
end

and this is how I'm using the inlive_svg method

    <a class="print-link"
       id="print-link-{{model.id}}"
       target="_self"
       title="Print {{model.title}}"
       ng-href="{{model.url}}?print=true"
       ng-show="model.extension === 'online-doc'" >
      <%= inline_svg('hub_v2/icons/Print.svg', class: 'print-link-icon') %>
    </a>
  </div>

I'm closing this issue since this has to do with angular-rails-templates.

Turns out that the issue indeed was on tilt, it wasn't loading the helpers, so this is the code that I used as a workaround

module CustomERBEngine
  class ERBTemplate < Tilt::ERBTemplate
    def evaluate(scope, locals, &block)
      scope.class_eval do
        include Rails.application.routes.url_helpers
        include Rails.application.routes.mounted_helpers
        include ActionView::Helpers
        include InlineSvg::ActionView::Helpers
      end

      super
    end
  end
end

Tilt.register CustomERBEngine::ERBTemplate, 'erb'

Thanks anyway @jamesmartin

@JonaMX thanks for following up with a detailed description of the root cause and providing your workaround to help others that stumble on the same problem. 👍 ⚡️