我有我的控制器

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] %>
理解?谢谢!啊!

最佳答案

试试这个
首先将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>

09-18 05:15