Convert rails object to javascript variables


#1

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


#2

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 removed_email_address@domain.invalid wrote:

%>
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


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


#3

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


#4

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


#5

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. removed_email_address@domain.invalid 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#6

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. removed_email_address@domain.invalid wrote:

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


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


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


#7

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:
http://www.bigbold.com/snippets/posts/show/474


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


#8

On 2/10/06, Mark Reginald J. removed_email_address@domain.invalid 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: http://www.bigbold.com/snippets/posts/show/474

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