1

Can I do escape_javascript with JSON respond?

I run Rails 4.2.1 and testing OAuth with Facebook. I've made link_to with remote true and ajax request with Facebook SDK.

  window.fbAsyncInit = ->
    FB.init
      appId: 'AppID'
      status: true
      cookie: true
      xfbml: true
    return

  ((d) ->
    js = undefined
    id = 'facebook-jssdk'
    if d.getElementById(id)
      return
    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
    return
  ) document
  $ ->
    $('#facebook, #vkontakte').click (e) ->
      e.preventDefault()
      FB.login ((response) ->
        if response.authResponse
          $('.modal').modal('hide')
          # $('#results').html 'Connected! Hitting OmniAuth callback (GET users/auth/facebook/callback)...'
          # since we have cookies enabled, this request will allow omniauth to parse
          # out the auth code from the signed request in the fbsr_XXX cookie

          $.getJSON '/profile/auth/facebook/callback', (json) ->
            # $('#results').html JSON.stringify(json)
            # Do some other stuff here (call more json, load in more elements, etc)
            return
        return
      ), scope: 'email'
      # These are the permissions you are requesting
      return
    $('#connect .signout').click (e) ->
      e.preventDefault()
      $.getJSON '/auth/facebook/signout', (json) ->
        $('#results').html JSON.stringify(json)
        return
      return
    return

Everything works fine, I got my user signed in with his account, however I need to make some front-end updates (i.e. re-render few partials etc..) I do like remote true and javascript_escape concept so I'd be happy to use same on JSON, however can't find any examples..

2 Answers 2

0

When you make a link remote true, your controller will by default attempt to render an action.js.erb view file where action is the name of the action you route to e.g. show.js.erb

In that view file you will write javascript and embedded Ruby much like in html.erb view files.

When you want to rerender a partial on the page you have to escape_javascript the result of the render method so that you can safely pass an html string to javascript.

$("#some-div").html("<%= escape_javascript render(partial: "some_content") %>");

Notice the erb tags are inside of quotes, this is so that you'll end up passing a string to the html function. You can also use the shortcut helper j

$("#some-div").html("<%= j render(partial: "some_content") %>");

http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html

Sign up to request clarification or add additional context in comments.

1 Comment

Well you are right, however - not. As you can see GetJSON will be landed as JSON in my controller and js.erb simply ignored. I came up with another idea. Check the below.
0

Finally came up with another idea, a little change of ajax js made trick.

$('#facebook, #vkontakte').click (e) ->
  e.preventDefault()
  FB.login ((response) ->
    $('.modal').modal('hide')
    # $('#results').html 'Connected! Hitting OmniAuth callback (GET users/auth/facebook/callback)...'
    # since we have cookies enabled, this request will allow omniauth to parse
    # out the auth code from the signed request in the fbsr_XXX cookie

    # $.getJSON '/profile/auth/facebook/callback', (json) ->
    $.get '/profile/auth/facebook/callback' if response.authResponse
  ), scope: 'email'
  # These are the permissions you are requesting
  return

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.