1

I'm working on a Rails project with a javascript asset relying on a route helper method.

In my .js.erb, I start with:

 <% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>

Then write the javascript itself:

  jQuery("#search_keyword")
  .bind("keydown", function(event) {
        if(event.keyCode === jQuery.ui.keyCode.TAB &&
           jQuery(this).data("ui-autocomplete").menu.active) {
                   event.preventDefault();
            }
  })
  .autocomplete({
        source: function (request, response) {
                jQuery.getJSON ( '<%= search_complete_belongings_path(:json) %>', {
                             term: extractLast (request.term)
                          }, response);
        },
        search: function() {
                var term = extractLast (this.value);
                if (term.length < 2) {
                   return false;
                }
        },
        focus: function(event, ui) {
            return false;
        },
        select: function(event, ui) {
            var terms = split (this.value);
            terms.pop(),
            terms.push(ui.item.belonging.name);
            this.value=terms;
            return false;
        }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li>" )
            .data( "item.autocomplete", item.belonging )
            .append( "<a>" + item.belonging.name + "</a>" )
            .appendTo( ul );
    };

This function relies on jquery-ui for the autocomplete function and makes an ajax call to the following route:

<%= search_complete_belongings_path(:json) %>

The code is working fine in dev but once I do the assets:precompile, I got the following error:

   NoMethodError: undefined method `search_complete_belongings_path' for #<#<Class:0x0000000279df18>:0x00000003813730>
   (in /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/app/assets/javascripts/belonging.js.erb)
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/app/assets/javascripts/belonging.js.erb:82:in `block in singletonclass'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/app/assets/javascripts/belonging.js.erb:65531:in `instance_eval'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/app/assets/javascripts/belonging.js.erb:65531:in `singletonclass'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/app/assets/javascripts/belonging.js.erb:65529:in `__tilt_4820660'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `call'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:170:in `evaluate'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:193:in `block in evaluate'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `each'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `evaluate'
   /tmp/build_d1c6dfa1-0995-4df1-9abe-74874ab13cb0/vendor/bundle/ruby/1.9.1/gems/sprockets-2.2.2/lib/sprockets/processed_asset.rb:12:in `initialize'

No Idea where it can come from?

Thanks!

microcosme
  • 703
  • 1
  • 5
  • 22

1 Answers1

0

you cannot use helpers, like search_complete_belongings_path in the app/assets, coz they're dynamic and work in the rails views only. your assets on the other hand are static and get precompiled once

Nikolay
  • 1,392
  • 1
  • 9
  • 15
  • It does seem that there is a way to use such helpers in assets. See e.g.: http://stackoverflow.com/questions/7451517/using-a-rails-helper-method-within-a-javascript-asset – microcosme Apr 15 '14 at 08:56