我有我的控制器
def grafico_gantt
mapa = Hash.new
mapa[:tasks] = []
@projeto.atividades.each do |a|
mapa[:tasks] << {
id:a.id,
descricao:a.descricao,
status:a.status,
data_inicial:a.data_inicial,
tempo_gasto:a.tempo_gasto.to_i,
data_final:a.data_final
}
end
end
我的档案.js.erb
<script>
$(function() {
var today = moment();
var andTwoHours = moment().add("hours",2);
var today_friendly = "/Date(" + today.valueOf() + ")/";
var next_friendly = "/Date(" + andTwoHours.valueOf() + ")/";
var maxDate = moment().add("days",10).toDate();
$(".gantt").gantt({
source: [{
name: <%= raw @mapa[:descricao] %>,
values: [{
from: today_friendly,
to: next_friendly,
label:"Teste",
customClass: "ganttRed"
}]
}],
});
});
</script>
如何制作代码名称:<%= raw @mapa[:descricao] %>
,
收到控制器值mapa[:descricao] = a.descricao
,
我只是在变量名中显示值,替换<%= raw @mapa[:descricao] %>
理解?谢谢!
答案 0 :(得分:2)
试试这个
首先将mapa转换为实例变量(@mapa)
然后相应地更新js.erb文件,如下所示。
<script>
$(function() {
var today = moment();
var andTwoHours = moment().add("hours",2);
var today_friendly = "/Date(" + today.valueOf() + ")/";
var next_friendly = "/Date(" + andTwoHours.valueOf() + ")/";
var maxDate = moment().add("days",10).toDate();
$(".gantt").gantt({
source: [{
name: '<%= raw @mapa[:tasks][0][:descricao] %>',
values: [{
from: today_friendly,
to: next_friendly,
label:"Teste",
customClass: "ganttRed"
}]
}],
});
});
</script>
答案 1 :(得分:0)
你的观点def grafico_gantt @mapa = Hash.new mapa[:tasks] = [] @projeto.atividades.each do |a| mapa[:tasks] << { id:a.id, descricao:a.descricao, status:a.status, data_inicial:a.data_inicial, tempo_gasto:a.tempo_gasto.to_i, data_final:a.data_final } end respond_to do |format| format.js end end
:
<%= @mapa[:task].first[:descricao] %>