3

I have ROR Helper that build some Javascript code. In the helper I have Hash of options and variables that define this javascript code. One of them is string that holds JS function, the problem is it rendered as a string and not as function when using to_json.

How can I make it work?

Example:

In my helper I have this code:

h = {url: '/some/url', async: false}
h[success] = "function(result) {alert(result);}"

"<script type='text/javascript'> jQuery.ajax(#{h.to_json}); </script>"html_safe

This code will generates:

<script type='text/javascript'>
  jQuery.ajax({
    url: '/some/url',
    async: false,
    success: "function(result) {alert(result);}"
  });
</script>

What I wont to to achieve is that code (without the ".." in success part):

<script type='text/javascript'>
  jQuery.ajax({
    url: '/some/url',
    async: false,
    success: function(result) {alert(result);}
  });
</script>
1
  • Why yes, JSON is not JavaScript, it doesn't have a concept of function, that makes sense. Commented Aug 25, 2017 at 10:52

3 Answers 3

2

You could create a string out of h hash instead of using to_json; for example:

def js_code
  h = {url: '"/some/url"', async: false}
  h[:success] = "function(result) { alert(result); }"
  s = h.map { |k, v| "#{k}: #{v}" }.join(",")

  "<script type='text/javascript'> jQuery.ajax({#{s}}); </script>".html_safe
end

Notice that additional double quotes (") were added to '"/some/url"' in order to keep them in the final string.

Output:

<script type='text/javascript'> jQuery.ajax({url: "/some/url",async: false, success: function(result) { alert(result); }}); </script>
Sign up to request clarification or add additional context in comments.

Comments

0

I would do that using heredoc syntax and string interpolation:

def some_helper_method
  h = { url: '/some/url', async: false }
  <<-HTML
    <script type='text/javascript'>
      jQuery.ajax({
        url: '#{ h[:url] }',
        async: #{ h[:async] },
        success: function(result) {
          alert(result);
        }
      });
    </script>
  HTML
end

Comments

0

The easiest way would be to remove the surrounding quotes with a regular expression like this.

"<script type='text/javascript'> jQuery.ajax(#{h.to_json}); </script>".gsub(/"success":"(.*)"/, '"success":\1')

which gives

<script type='text/javascript'> jQuery.ajax({"url":"/some/url","async":false,"success":function(result) {alert(result);}}); </script>

Which is not complety what you want. I would just build a string instead of using the json approach.

html = %Q{
  <script type='text/javascript'>
    jQuery.ajax({
      url: '#{h[:url]}',
      async: #{h[:async]},
      success: #{h[:success]}
    });
  </script>
}

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.