使用.js.erb

时间:2013-07-29 16:48:34

标签: javascript ruby-on-rails ruby controller

我有我的控制器

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] %>

理解?谢谢!

2 个答案:

答案 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)

  1. 在您的控制器上:
  2. 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] %>