I am trying to have a json response in which some value is html rendered by a partial
#projects_Controller.rb
def index
respond_to do |f|
f.json
end
end
# index.json.erb
{
"html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer') %>"
}
But I get the following error:
ActionView::Template::Error (Missing partial projects/disclaimer with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>
[:json], :locale=>[:en, :en]} in view paths "c:/rails/app/views", "c:/rails/vendor/plugins/more/app/views", "C:/Ruby192/lib/ruby/gems/1.9.1/gems/devise-1.1.8/app/views")
It appears JSON requests renders partial with .json.erb in its name but not .html.erb, which is what I have. Is there a way for me to specify 'html'.
ADDED: If the request is 'js', and in index.js.erb I render the almost same code: # index.js.erb
disclaimer = {
"html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer') %>"
}
it does find the projects/disclaimer.html.erb and renders it correctly. I wonder why is there such an inconsistency in that if one requested js, any partial rendering in its template will look for partial_name.html.erb but if one requested json, the partial rendering would ask for partial_name.json.erb?
Thank you
Got it: All one needs is this line in the .json.erb file <% self.formats = ["html"] %>
So the full index.json.erb
For future readers, you may pass on formats parameter like this.
I'm not sure if I got you right, but maybe you can play with the content type like this:
The :locals is just if you want to pass an var to the partial.
My answer is similar to Nik's above. I have the following helper for json.erb templates:
So now I can write templates like
This would be nicer if actionview allowed rendering with a content_type like in 23tux's example (which doesn't work for me). It would also be nicer if only *.html.erb underwent html escaping instead of all *.erb files.