4

I want to post some data as an html attribute. Suppose there is a rails variable

@fields = [{:name => "id", :type => :integer}]

I want to post in the page for use with jquery's .data(). So in the .html.erb I have

<%= form_tag( {:action => 'whatever'}, {... , :"data-fields" => h(@fields.to_json)}) %>

But while rendering, the quotations in string [{"name":"id","type":"integer"}] mess up the other attributes because Rails form_tag uses double quotes to enclose the whole json string in double quotes. How do I post json with strings as an attribute from Rails?

highBandWidth
  • 16,751
  • 20
  • 84
  • 131

5 Answers5

0

Have you tried the following?

<%= form_tag { :action => "whatever" }, { :data => { :fields => h(@fields.to_json) } } %>
Theo Scholiadis
  • 2,316
  • 2
  • 22
  • 33
0

Here is how I side-stepped the problem:

In the form tag, I did :"data-fields" => @fields.to_json.gsub('"',"'").

This produces HTML of this sort:

"data-fields"="[{'name':'id','type':'integer'}]"

And then, in the JS, I retrieve it like this:

$.parseJSON($('form').data('fields').replace(/'/g,'"'))
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
  • 1
    I know this might be a bit of a "sideline", but have you considered sending each item in the json file as a separate data- field? E.g. "data-id" and "data-type". I'm posting this as a comment, as it is not the answer to your question, but still a possible solution to your problem. I'm just thinking that you might be over-complicating the matter when you could look for a simpler solution. And by no means am I saying that mine is a perfect one... – Theo Scholiadis Mar 03 '13 at 21:03
  • it;s a possible work around in this case, I have an array `[{id:1,type:a},{id:2,type:z},...]` so I could make two arrays and then use the index to link them. In the general case though there should be a way of attaching valid json attributes. – highBandWidth Mar 05 '13 at 17:12
  • Technically, you're right. It should be possible. However, logically, it's not something that takes place frequently so it probably didn't cross their mind lol. If you do figure it out, please let us know. It's quite useful :P – Theo Scholiadis Mar 05 '13 at 18:42
0

Have you tried escape_javascript ? Although it has known downsides, it is not exactly for escaping JSON, and I am not sure escaped quotes will work in HTML attributes.

Community
  • 1
  • 1
khustochka
  • 2,346
  • 20
  • 29
0

There is an episode on railscasts addressing your situation using a serializer. The point is that the serializer replaces double quotes with $quot; The serialuzer also gives you the ability to select which attributes to serialize and include associations.

Andreyy
  • 511
  • 2
  • 11
  • The serializer is for activerecords. How do I simply write the json for a rails hash/array as in the question inside an erb file? – highBandWidth Mar 11 '13 at 18:28
0

After some trial and error this is what works.

Server side:

<% @fields = [{:name => "id", :type => :integer}] %>
<%= form_tag( '/posts/new', {id:'example',:data => { :fields=>@fields}}) %>

Generated HTML:

<form accept-charset="UTF-8" action="/posts/new" data-fields="[{&quot;name&quot;:&quot;id&quot;,&quot;type&quot;:&quot;integer&quot;}]" id="example" method="post">

Javascript wiht JQuery's data method

fields = $('#example').attr('data-fields')

I'm using rails 2.3.8 and jquery-rails 2.2.1

Andreyy
  • 511
  • 2
  • 11
  • Doesn't work for me. The generated HTML is `
    ` the double quotes everywhere mess things up and hence the browser interprets it as `
    `
    – highBandWidth Mar 13 '13 at 16:58