1

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"
      };
Community
  • 1
  • 1
Tebbers
  • 484
  • 7
  • 12

2 Answers2

4

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]
    };
sidney
  • 2,704
  • 3
  • 28
  • 44
  • That fixed it, thanks very much! I was also using the wrong variables - I actually needed #{@current_user.email}. I have a new problem though - if no user is signed in, Slim throws an error as there is no variable for the current user and the page doesn't load. What can I do now?! – Tebbers Mar 13 '14 at 00:50
  • you can set this before loading your code : `current_user ||= ''` It will force the current_user to get an empty variable (that won't work if you use a method on current_user) You could also set a ternary expression to test if the current user exists: `current_user ? current_user.email : ''` (best solution I think) Last but not not least, if you only use current_user for a few method, you can do this for each method: `current_user.try(:email)`, to prevent any exception. – sidney Mar 13 '14 at 08:39
0

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!

Tebbers
  • 484
  • 7
  • 12
  • I'm glad to have helped you ;) Between, in StackOverflow, as someone who asked a question (unless nobody answered the right thing), you should not post an answer, but edit your first post to show that for instance. – sidney Mar 14 '14 at 09:17