Now this has had me tearing my hair out for two nights - I'm in the process of installing the JavaScript for Intercom.io, which requires a snippet of code to be placed before the closing </body> tag, like so:

<script id="IntercomSettingsScriptTag">
window.intercomSettings = {
// TODO: The current logged in user's email address.
email: "john.doe@example.com",
// TODO: The current logged in user's sign-up date as a Unix timestamp.
created_at: 1234567890,
app_id: [redacted]
</script>

Sounds easy, but my issue is that I need to use a Ruby variable to swap in the current logged in user's email address into the email field - it should be just a simple #{@user.full_name} (or at least I think it is, I'm just a beginner).

However, Slim is preventing me from doing so as it doesn't execute the variable and it just prints the entire code snippet as-is in the browser's source code. I think the issue is similar to this one: How to access instance variables in CoffeeScript engine inside a Slim template

Here's my Slim code:

|<script id="IntercomSettingsScriptTag">
 window.intercomSettings = {
 // TODO: The current logged in user's email address.
 email: "#{@user.email}",
 // TODO: The current logged in user's sign-up date as a Unix timestamp.
 created_at: #{@user.created_at.to_i},
 app_id: [redacted]
 };
 </script>

The above crashes Slim and the page won't even load. I have tried declaring the variables before the snippet using the javascript: Slim function too, as in the above link. No luck.

Has anyone else got any ideas on how to pass the variables into the JavaScript? Would be so grateful for some pointers. Thanks!

SOLVED

This is the code that worked:

- if user_signed_in?
  script id="IntercomSettingsScriptTag"
    |
      window.intercomSettings = {
      // TODO: The current logged in user's full name
      name: "#{@current_user.full_name}",
      // TODO: The current logged in user's email address.
      email: "#{@current_user.email}",
      // TODO: The current logged in user's sign-up date as a Unix timestamp.
      created_at: #{@current_user.created_at.to_i},
      app_id: "eac384da45babdcac214d669601f1a29632f0d97"
      };
有帮助吗?

解决方案

This will probably fix it

script id="IntercomSettingsScriptTag"
  |
    window.intercomSettings = {
      // TODO: The current logged in user's email address.
      email: "#{@user.email}",
      // TODO: The current logged in user's sign-up date as a Unix timestamp.
      created_at: #{@user.created_at.to_i},
      app_id: [redacted]
    };

其他提示

Yes, thanks very much for the tip! This is what finally worked for me:

- if user_signed_in?
  script id="IntercomSettingsScriptTag"
    |
      window.intercomSettings = {
      // TODO: The current logged in user's full name
      name: "#{@current_user.full_name}",
      // TODO: The current logged in user's email address.
      email: "#{@current_user.email}",
      // TODO: The current logged in user's sign-up date as a Unix timestamp.
      created_at: #{@current_user.created_at.to_i},
      app_id: "eac384da45babdcac214d669601f1a29632f0d97"
      };

I'm only a beginner so it's really exciting to see it working properly. Magical!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top