json erb模板找不到其他html部分

发布于 2024-10-25 18:41:05 字数 1076 浏览 3 评论 0原文

我试图有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我最亲爱的 2024-11-01 18:41:05

明白了:只需要 .json.erb 文件中的这一行
<% self.formats = ["html"] %>

所以完整的index.json.erb

      <% self.formats = ["html"] %>
      disclaimer = {
        "html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer',
                    :content_type => 'text/html'), 
                    :locals => {:localVariable => @localVariable} 
                %>"
      }

Got it: All one needs is this line in the .json.erb file
<% self.formats = ["html"] %>

So the full index.json.erb

      <% self.formats = ["html"] %>
      disclaimer = {
        "html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer',
                    :content_type => 'text/html'), 
                    :locals => {:localVariable => @localVariable} 
                %>"
      }
新雨望断虹 2024-11-01 18:41:05

我的回答与上面尼克的类似。我有以下 json.erb 模板的帮助器:

# helpers useful for json.erb templates
module JsonHelper

  # Same as render but force actionview to look for html templates instead of json.
  def render_html(options={}, locals={}, &block)
    old_formats = formats
    self.formats = [:html] # hack so partials resolve with html not json format
    render options, locals, &block  

  ensure
    self.formats = old_formats 
  end 

  # json escape a string. For example <%=json "some { string }" %>
  def json(value)
    raw value.to_json
  end 
end

所以现在我可以编写这样的模板,

{
  "html": <%=json render_html(:partial => 'some_partial') %>,
  "status": success
}

如果 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:

# helpers useful for json.erb templates
module JsonHelper

  # Same as render but force actionview to look for html templates instead of json.
  def render_html(options={}, locals={}, &block)
    old_formats = formats
    self.formats = [:html] # hack so partials resolve with html not json format
    render options, locals, &block  

  ensure
    self.formats = old_formats 
  end 

  # json escape a string. For example <%=json "some { string }" %>
  def json(value)
    raw value.to_json
  end 
end

So now I can write templates like

{
  "html": <%=json render_html(:partial => 'some_partial') %>,
  "status": success
}

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.

万劫不复 2024-11-01 18:41:05

对于未来的读者,您可以像这样传递格式参数。

= render partial: 'user', locals: {xyz: @xyz}, :formats => [:html]

For future readers, you may pass on formats parameter like this.

= render partial: 'user', locals: {xyz: @xyz}, :formats => [:html]
农村范ル 2024-11-01 18:41:05

我不确定我是否正确,但也许您可以使用这样的内容类型:

disclaimer = {
  "html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer', :content_type => 'text/html'), :locals => {:localVariable => @localVariable} %>"
}

:locals 只是如果您想将 var 传递给部分。

I'm not sure if I got you right, but maybe you can play with the content type like this:

disclaimer = {
  "html":"<%= raw escape_javascript(render :partial => 'projects/disclaimer', :content_type => 'text/html'), :locals => {:localVariable => @localVariable} %>"
}

The :locals is just if you want to pass an var to the partial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文