0

I have source code for an app that uses Ember,CoffeeScript, Haml with Rails. In the layout file, it initiates the app with this code, which has a current_user_json helper method written in Ruby

:javascript
    App.ready = function() {
      App.initApp(jQuery.parseJSON('#{current_user_json}'))
    }

Trying to recreate the app with erb and JavaScript, I get an unexpected token error

Uncaught SyntaxError: Unexpected token # 

If I do the following

  <script type="text/javascript">
 App.ready = function() {
      App.initApp(jQuery.parseJSON('#{ current_user_json}'));
    }
  </script>

Why is that token unexpected and how can I avoid that error?

1
  • #{ variable } interpolation is only for HAML. Commented Jan 26, 2014 at 23:24

2 Answers 2

1

I can't tell if it's not working for you in HAML or not based on your question. I would assume it is, because I use that same method as well and it works fine.

As for ERB, I don't personally use it but I don't think you can just drop a string in like that, try this:

<script type="text/javascript">
  App.ready = function() {
    App.initApp(jQuery.parseJSON('<%= current_user_json %>'));
  }
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Change it to double quotes.

App.initApp(jQuery.parseJSON("#{ current_user_json}"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.