预编译时未在javascript资源中找到路由帮助程序方法
我正在处理一个Rails项目,该项目使用一个javascript资产,该资产依赖于route helper方法 在my预编译时未在javascript资源中找到路由帮助程序方法,javascript,ruby-on-rails,ruby,asset-pipeline,Javascript,Ruby On Rails,Ruby,Asset Pipeline,我正在处理一个Rails项目,该项目使用一个javascript资产,该资产依赖于route helper方法 在my.js.erb中,我从以下内容开始: <% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %> 不知道它从哪里来 谢谢 您不能在应用程序/资产中使用帮助程序,如搜索完整的物品路径,因为它们是动态的,只能在rails视图中工作。另一方面
.js.erb
中,我从以下内容开始:
<% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>
不知道它从哪里来
谢谢 您不能在
应用程序/资产
中使用帮助程序,如搜索完整的物品路径
,因为它们是动态的,只能在rails视图中工作。另一方面,您的资产是静态的,只需预编译一次似乎有一种方法可以在资产中使用此类帮助程序。例如,见:
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 );
};
<%= search_complete_belongings_path(:json) %>
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'