Forum: Ruby on Rails convert rails object to javascript variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
M Daggett (Guest)
on 2006-02-10 08:16
(Received via mailing list)
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

--
Dan W. (Guest)
on 2006-02-10 15:42
(Received via mailing list)
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
Gregory S. (Guest)
on 2006-02-10 15:47
(Received via mailing list)
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 </script> 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
Gregory S. (Guest)
on 2006-02-10 16:34
(Received via mailing list)
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
Dan W. (Guest)
on 2006-02-10 16:45
(Received via mailing list)
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
M Daggett (Guest)
on 2006-02-10 20:22
(Received via mailing list)
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
>


--
Mark Reginald J. (Guest)
on 2006-02-11 11:18
(Received via mailing list)
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.
BigSmoke (Guest)
on 2006-02-15 19:19
(Received via mailing list)
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
This topic is locked and can not be replied to.