控制器中的 render_to_string 部分格式错误 [英] render_to_string partial format error in controller

查看:49
本文介绍了控制器中的 render_to_string 部分格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器上运行以下代码时出现以下错误.请注意错误中的 :formats=>[:json],即使 :formats=>[:html] 被传递到 render_to_string

I get the below error when running the below code on a controller. Please note the :formats=>[:json] in the error, even though :formats=>[:html] is passed into render_to_string

我做错了什么?有任何想法吗?实际上,下面的代码以前运行良好,不确定是什么更改影响了此错误.Rails 版本:3.2.8

What am I doing wrong? Any ideas? Actually, the code below worked fine before, not sure what changes influenced this error. Rails version: 3.2.8

顺便说一句,模板绝对到位:loc/_search_details.html.erb

btw the template is definitely in place: loc/_search_details.html.erb

额外问题:我在哪里可以找到显示可以传递给 render_to_string 的参数及其工作原理的 api 文档?

Bonus question: where can I find the api documentation showing what parameters can be passed to render_to_string and how it works?

错误:ActionView::MissingTemplate(缺少部分 loc/search_details 和 {:locale=>[:en]、:formats=>[:json]、:handlers=>[:erb、:builder、:coffee]}.

  respond_to do |format|
    format.json { 
      @detail_str = render_to_string(:partial => 'loc/search_details', :layout => false, :formats=>[:html], :locals => {:beer_results => @beer_results})
      @list_str = render_to_string(:partial => 'loc/search_list', :layout => false,:formats=>[:html], :locals => {:beer_results => @beer_results})
      render :json => {:results => @results_hash, :result_details => @detail_str, :result_list => @list_str }

      }
  end

推荐答案

参见 这个问题这个问题.由于您正在发出 JSON 请求,您的 render_to_string 期望您的部分是 _search_details.json.erb.您可以提供额外的 JSON 部分,重命名部分或将其添加到部分 <% self.formats = [:json, :html] %>,或者,尝试解决方法该问题的公认答案.

See this question and this issue. Since you're making a JSON request your render_to_string is expecting your partial to be _search_details.json.erb. You can either provide an additional JSON partial, rename the partial or add this to the partial <% self.formats = [:json, :html] %>, or alternatively, try the workaround in the accepted answer to that question.

这篇关于控制器中的 render_to_string 部分格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆