Home > Net >  How to use a rails condition within a .slim Javascript function?
How to use a rails condition within a .slim Javascript function?

Time:05-10

In my .slim file I have a javascript block containing a URL, this URL needs to differ depending on which environment it is rendered in (staging / production etc). This is my first time using this format, so I'm not sure if what I'm trying to do is logically possible.

I aim to create a ternary operator which checks the Rails Env, if it is 'Production' do X, otherwise do Y.

Code below:

file.slim

        javascript:
      function zopimChat() {
        window.zEmbed || function (e, t) {
          var n, o, d, i, s, a = [],
              r = document.createElement("iframe");
          window.zESettings = {
            webWidget: {
              offset: {
                vertical: "85px"
              }
            }
          };
          window.zEmbed = function () {
            a.push(arguments)
          }, window.zE = window.zE || window.zEmbed, r.src = "javascript:false", r.title = "", r.role = "presentation", (r.frameElement || r).style.cssText = "display: none", d = document.getElementsByTagName("script"), d = d[d.length - 1], d.parentNode.insertBefore(r, d), i = r.contentWindow, s = i.document;
          try {
            o = s
          } catch (c) {
            n = document.domain, r.src = 'javascript:var d=document.open();d.domain="'   n   '";void(0);', o = s
          }
          o.open()._l = function () {
            var o = this.createElement("script");
            n && (this.domain = n), o.id = "js-iframe-async", o.src = e, this.t =  new Date, this.zendeskHost = t, this.zEQueue = a, this.body.appendChild(o)
          }, o.write('<body onl oad="document._l();">'), o.close()
        }

    # condition below
     
       ("https://assets.zendesk.com/embeddable_framework/main.js", 
        Rails.env.production? "testwebsite.zendesk.com" : "testwebsite-staging.zendesk.com" );
        }

CodePudding user response:

You can use interpolation:

javascript:
  const railsEnv = "#{Rails.env}";

https://rdoc.info/gems/slim/frames#text-interpolation

In your case:

("https://assets.zendesk.com/embeddable_framework/main.js", 
  "#{Rails.env.production? ? "testwebsite.zendesk.com" : "testwebsite-staging.zendesk.com"}" );
  • Related