我想给filename
JavaScript变量注入到我的Ruby代码是这样的:如何使用HAML将JavaScript变量注入Ruby代码?
"#{ajax_download_report_url(" + filename+ ")}"
这是一个基本的路径URL,但我需要通过这个变量HAML,因为我从这个Ajax请求得到它:
:javascript
var updaterId = setInterval(function() {
$.ajax({
url: #{admin_reports_url.to_json},
dataType: "json",
type: "GET",
data: {job_id: "#{job_id}"},
success: function(response){
var filename = response.file;
switch(status) {
case 'failed':
$('.loader').hide();
clearInterval(updaterId);
alert(message);
break;
case 'completed':
$('.loader').hide();
clearInterval(updaterId);
location.href = "#{ajax_download_report_admin_reports_url(" + filename+ ")}";
break;
}
}
})}, 1000);
现在我有一个错误:
ActionController::RoutingError - No route matches
{:controller=>"admin/reports", :action=>"ajax_download_report", :file=>" + filename+ "}
这意味着HAML不能正常evalute此代码。
它看起来并不像你试图注入一个JavaScript变量为Ruby代码,这是行不通的。相反,它看起来像你想要在一些JavaScript代码中插入一个Ruby变量。 –
@锡文,var文件名向上。 – 7stud
是的,那些Ruby字符串是通过'#{...}'内部的字符串插值注入到JavaScript代码中的。 –