Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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: "[email protected]",
// 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"
      };
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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]
    };
share|improve this answer
    
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?! –  user3052139 Mar 13 at 0: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 at 8:39
add comment

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!

share|improve this answer
    
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 at 9:17
1  
OK thanks @sidney, I'll edit my first question then. Cheers! –  user3052139 Mar 15 at 10:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.