1

I'd like to include this Coffee script code in the header of my application.html.haml but I don't know where to put it or how to include ruby objects.

Question 1. How do I render and Javascript .js or Coffeescript .coffee that lives in another file? (where do I put the file, in the assets? in the shared folder?)

Question 2. How do you mixing variables that are in ruby (globals ENV['APP_ID'] for example) see code below:

    window.fbAsyncInit = ->
  FB.init
    appId: "#{ENV['APP_ID']}"
    status: true
    cookie: true
    oauth: true
    xfbml: true

((d) ->
  js = undefined
  id = "facebook-jssdk"
  return  if d.getElementById(id)
  js = d.createElement("script")
  js.id = id
  js.async = true
  js.src = "//connect.facebook.net/en_US/all.js"
  d.getElementsByTagName("head")[0].appendChild js
) document
$ ->
  $("a").click (e) ->
    e.preventDefault()
    FB.login ((response) ->
      $.get "/auth/facebook/callback"  if response.authResponse
    ),
      scope: "#{SCOPE}"

1 Answer 1

2

Q1. I wouldn't recommend rendering partials of inline javascript within other javascript, but if you are rendering templates then you should to keep them in app/assets/templates

Q2. If you are using the Rails Asset Pipeline, you can add additional extensions to put the file through additional parsers. So add an ERB extension to render ruby variables in JS. eg.

#my_file.js.coffee.erb
window.fbAsyncInit = ->
  FB.init
    appId: "<%= ENV['APP_ID'] %>"
    status: true
...
Sign up to request clarification or add additional context in comments.

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.