json erb模板找不到其他html部分
我试图有一个 json 响应,其中某些值是由部分呈现的 html
#projects_Controller.rb
def index
respond_to do |f|
f.json
end
end
# index.json.erb
{
"html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer') %>"
}
但我收到以下错误:
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")
看来 JSON 请求呈现名称中带有 .json.erb 但不是 .html.erb 的部分,这就是我的内容有。 有没有办法让我指定“html”。
额外: 如果请求是“js”,并且在index.js.erb中我呈现几乎相同的代码: #index.js.erb
disclaimer = {
"html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer') %>"
}
它确实找到了projects/disclaimer.html.erb并正确呈现它。我想知道为什么会存在这样的不一致:如果请求 js,则其模板中的任何部分渲染都会查找partial_name.html.erb,但如果请求json,则部分渲染会请求partial_name.json.erb?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
明白了:只需要 .json.erb 文件中的这一行
<% self.formats = ["html"] %>
所以完整的index.json.erb
Got it: All one needs is this line in the .json.erb file
<% self.formats = ["html"] %>
So the full index.json.erb
我的回答与上面尼克的类似。我有以下 json.erb 模板的帮助器:
所以现在我可以编写这样的模板,
如果 actionview 允许像 23tux 的示例中那样使用 content_type 进行渲染(这对我来说不起作用),那就更好了。如果只有 *.html.erb 而不是所有 *.erb 文件进行 html 转义,那就更好了。
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.
对于未来的读者,您可以像这样传递格式参数。
For future readers, you may pass on formats parameter like this.
我不确定我是否正确,但也许您可以使用这样的内容类型:
:locals 只是如果您想将 var 传递给部分。
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.