1
我正在使用依賴於路由幫助器方法的JavaScript資源來處理Rails項目。預編譯時未找到javascript資源中的路徑幫助器方法
在我.js.erb
,我開始:
<% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>
然後寫的JavaScript本身:
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);
};
此功能依賴於jQuery的UI的自動完成功能,使一個AJAX調用以下路徑:
<%= search_complete_belongings_path(:json) %>
該代碼在dev中工作正常•一旦我做assets:precompile
,我得到了以下錯誤:
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'
不知道在哪裏可以從何而來?
謝謝!
似乎有一種方法可以在資產中使用這種助手。參見例如:http://stackoverflow.com/questions/7451517/using-a-rails-helper-method-within-a-javascript-asset – microcosme