简体   繁体   English

如何将 Ruby 会话变量传递给 JavaScript 标记

[英]How to pass a Ruby session variable to a JavaScript tag

I am working with a vendor that uses a JavaScript tag for sale attribution.我正在与使用 JavaScript 标签进行销售归因的供应商合作。 I need to pass session variables to the tag.我需要将会话变量传递给标签。 The tag is firing, and I can see the variables in the logs, but not in the tag.标记正在触发,我可以在日志中看到变量,但在标记中看不到。

I verified that the session variable is set by adding puts{variable} to the controller and see the proper values in the log, but not able to get the variables in the JavaScript tag.我通过向控制器添加puts{variable}并在日志中看到正确的值来验证会话变量是设置的,但无法获取 JavaScript 标记中的变量。

This is the Tag code:这是标签代码:

:javascript

  /* Extole Script */
  (function(c,e,k,l,a){c[e]=c[e]||{};for(c[e].q=c[e].q||[];a<l.length;)k(l[a++],c[e])})(window,"extole",function(c,e){e[c]=e[c]||function(){e.q.push([c,arguments])}},["createZone"],0);
  /* End Extole Script */
   extole.createZone({
     name: 'conversion',
     data: {
       "first_name" = <%= fist_name %>,
       "last_name" = <%= last_name%>,
       "email" = <%= email_address>,

     }
  });

This is the Controller code:这是控制器代码:

  def index

    if session['cart_products'] == nil
      session['cart_products'] = {}
    end
    puts "\n\nCoupon\n\n"
    puts session['coupon_code']
    puts session['email_address']
    puts session['first_name']
    puts session['last_name']
    puts session['carttotal']
    puts "\n\n"
    @cart_size = session['cart_products'].length

  end

The logs show the session['first_name'] value.日志显示session['first_name']值。

您是否尝试在视图中使用相同的代码,例如:

"first_name" = <%= session["fist_name"] %>

It looks like you're using HAML for your template (judging by the :javascript filter), but you're trying to use ERB interpolation inside.看起来您正在为模板使用 HAML(根据:javascript过滤器判断),但您正在尝试在内部使用 ERB 插值。

You need to use proper HAML interpolation:您需要使用适当的 HAML 插值:

:javascript

  /* Extole Script */
  (function(c,e,k,l,a){c[e]=c[e]||{};for(c[e].q=c[e].q||[];a<l.length;)k(l[a++],c[e])})(window,"extole",function(c,e){e[c]=e[c]||function(){e.q.push([c,arguments])}},["createZone"],0);
  /* End Extole Script */
   extole.createZone({
     name: 'conversion',
     data: {
       "first_name" = '#{fist_name}',
       "last_name" = '#{last_name}',
       "email" = '#{email_address}',

     }
  });

The surrounding single quotes are there for JS to interpret these as strings.周围的单引号供 JS 将它们解释为字符串。

Documentation: https://haml.info/docs/yardoc/file.REFERENCE.html#ruby-interpolation-文档: https : //haml.info/docs/yardoc/file.REFERENCE.html#ruby-interpolation-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM