Paul,

thanks for the suggestion. I was hoping there is something in Rails to do
that for me. But I guess there isn't.

Other suggestion I got, off this list, is to have JSON embedded in a
javascript string, and parsed by jQuery into an object. With proper
javascript escaping (by j method), that gets the job done. And I like that
approach better then calling h on attributes on server side, as this is more
generic IMO, since it will work for any Ruby object.

Here is the code that works:

<% tags = [{name:"tag1", color:"green"}, {name:"</script><b>I can \n\ndo
something bad here</b>", color:"red"}] %>

<script type="text/javascript" charset="utf-8">
  //<![CDATA[
  var tags_list = $.parseJSON('<%=j tags.to_json.html_safe %>');
  // ]]>
</script>


And that results in:


<script type="text/javascript" charset="utf-8">
  //<![CDATA[
  var tags_list =
$.parseJSON('[{\"name\":\"tag1\",\"color\":\"green\"},{\"name\":\"<\/script><b>I
can \\n\\ndo something bad here<\/b>\",\"color\":\"red\"}]');
  // ]]>
</script>

Thanks all!

On Fri, Aug 26, 2011 at 4:43 PM, Peter Hickman <
peterhickman...@googlemail.com> wrote:

> The purpose of JSON is to be able to interchange the data between
> various systems. As you are not going to do that then why use JSON at
> all?
>
> Honestly if you are going to process the JSON with Javascript what
> dont you write your data out as Javascript data structures (which it
> would be converted from JSON anyway) in the page and process that?
> What do you gain from embedding the JSON then having to convert it
> into Javascript data structures over just writing the native
> Javascript data structures?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to