I've created a json view with JBuilder. But I want to preload this into a data object, so Backbone has access to the data early on without fetching for it.
How can I render the list.json.jbuilder view into my list.html.erb view?
Normally without jbuilder, I'd do something like this:
<div data-list="<%= @contents.to_json %>"></div>
render
, when called from within a view, returns a string rendering of the passed template or partial; you can embed that string into your view as you like. Note though that:
list.html.erb
instead of list.json.jbuilder
. If you're making this call from list.html.erb
, trying to render list.html.erb
results in infinite recursion and a SystemStackError. Using the :format
option for render
doesn't appear to work.list.json.jbuilder
resides in the same directory as list.html.erb
. render
call through raw
; otherwise, it will get escaped when it gets embedded into the view.So, for your example, you might write this, assuming your templates were in /app/views/foo
:
<div data-list="<%= raw render(:template => "foo/list.json", :locals => { :contents => @contents }) %>"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With