Convert rails object to javascript variables

Am i a total idiot to try and parse out a rails object for javascript
in this way?

<%
for i in interface_items[0].attributes
%>
var <%= i[0] %> = “<%=h( i[1] )%>”
<%
end
%>

Am I missing something really obvious?

Thanks,
Mark

A nicer way to do this would be to turn your object into JSON with
ruby-json:

gem install ruby-json

JSON is actually valid javascript so you can then do something like
this:
<%
require_gem ‘ruby-json’
%>

var data = <%= interface_items.to_json %>;

then the javascript varaible data would contain a representation of
the object which looks like it would be an array in this case.

Cheers,

Dan

On 2/10/06, M Daggett [email protected] wrote:

%>
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net

On Thu, Feb 09, 2006 at 10:16:32PM -0800, M Daggett wrote:
} Am i a total idiot to try and parse out a rails object for javascript
} in this way?
}
} <%
} for i in interface_items[0].attributes
} %>
} var <%= i[0] %> = “<%=h( i[1] )%>”
} <%
} end
} %>

That should probably work, but you are also probably better off with
JSON
http://www.json.org/:

module IncludeThisInYourHelper
def obj_to_simple_JSON(obj)
q = ‘"’
json = ‘{ ’
for i in obj.attributes
#escape single quotes; a tag would also be bad
value = i[1].to_s.gsub(/’/, “‘+#{q}’#{q}+'”)
json << “'#{i[0]} : ‘#{value}’”
end
return json.sub(/, $/, ’ }')
end
end

…and in your .rhtml…

var myobject = <%= obj_to_simple_JSON %>;

} Am I missing something really obvious?

Just that it’s better to put such things in helpers, usually.

} Thanks,
} Mark
–Greg

On Fri, Feb 10, 2006 at 01:41:44PM +0000, Dan W. wrote:
} A nicer way to do this would be to turn your object into JSON with
ruby-json:
}
} gem install ruby-json
}
} JSON is actually valid javascript so you can then do something like
this:
} <%
} require_gem ‘ruby-json’
} %>
}
} var data = <%= interface_items.to_json %>;
}
} then the javascript varaible data would contain a representation of
} the object which looks like it would be an array in this case.

Oh, COOL! I thought I was being spiffy with my quickie JSON output, but
that’s much better. Thanks for bringing it to my (our) attention.

} Cheers,
} Dan
–Greg

Hey Dan and Gregory,
That was a great help. I was able to get access to my variables 20
seconds after I installed the GEM. Thanks so much for pointing me in
the right direction.

Mark

On 2/10/06, Dan W. [email protected] wrote:

} JSON is actually valid javascript so you can then do something like this:
that’s much better. Thanks for bringing it to my (our) attention.


Dan W.
http://www.danwebb.net


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

It works straight out of the box on hashes and arrays but if you want
to serialize a whole object you need to make your own to_json method
but its not difficult.

On 2/10/06, Gregory S. [email protected] wrote:

} var data = <%= interface_items.to_json %>;


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Dan W.
http://www.danwebb.net

Dan W. wrote:

It works straight out of the box on hashes and arrays but if you want
to serialize a whole object you need to make your own to_json method
but its not difficult.

There’s this AR->JSON function:


We develop, watch us RoR, in numbers too big to ignore.

On 2/10/06, Mark Reginald J. [email protected] wrote:

Dan W. wrote:

It works straight out of the box on hashes and arrays but if you want
to serialize a whole object you need to make your own to_json method
but its not difficult.

There’s this AR->JSON function: BigBold - Informasi Tentang Bisnis dan Marketing

Extremely useful, as is the rest of this thread. I’ve been looking
all day - enough time to write a few JSON generators - for the easiest
way of turning some AR objects or simple hashes into simple JavaScript
(JSON).

Cheers to everyone who has carried solution into this thread,

  • Rowan Rodrik