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

时间:2019-07-09 17:32:47

标签: javascript ruby tags haml

我正在与使用JavaScript标签进行销售归因的供应商合作。我需要将会话变量传递给标签。标签正在触发,我可以在日志中看到变量,但在标签中看不到。

我验证了通过向控制器添加class MapDateSlider extends MapControl { createLeafletElement(props) { const control = L.control({ position: 'bottomleft' }) const jsx = ( <div onMouseOver={() => { props.leaflet.map.dragging.disable() }} onMouseOut={() => { props.leaflet.map.dragging.enable() }} > <DateSlider {...props} /> </div> ) control.onAdd = () => { const div = L.DomUtil.create('div', '') ReactDOM.render(jsx, div) return div } return control } } export default withLeaflet(withStyles(eventsPageStyle)(MapDateSlider)) 来设置会话变量,并在日志中看到正确的值,但无法在JavaScript标记中获取变量的情况。

这是标记代码:

  [HttpGet("GameDownloadLinks/{libraryId}")]
    public async Task<ActionResult<IEnumerable<GameDownloadLinks>>> GetGameLinksForlibraryAsync(Guid libraryID)
    {
        var libraryGameLinks = await (from gk in _context.GameLinks
                                   join gl in _context.GameList on gk.GameId equals gl.GameId
                                   where gl.libraryId == libraryId
                                   select new GameDownloadLinks
                                   {
                                       LibraryId = gl.libraryId,
                                       LinkText = gk.LinkText,
                                       Price = gk.Price,
                                       GameId = gl.GameId
                                   }).ToListAsync();

       // var asyncGameDownloadLinks = await Task.WhenAll(libraryGameLinks).toListAsync();
        return libraryGameLinks ;
    }

这是控制器代码:

puts{variable}

日志显示: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>, } }); 值。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您的模板似乎使用了 HAML(根据 :javascript 过滤器判断),但您正在尝试在内部使用 ERB 插值。

您需要使用适当的 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}',

     }
  });

周围的单引号用于 JS 将它们解释为字符串。

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