How do I render a partial of a different format in Rails?

前端 未结 11 1276
执念已碎
执念已碎 2020-11-28 04:21

I\'m trying to generate a JSON response that includes some HTML. Thus, I have /app/views/foo/bar.json.erb:

{
  someKey: \'some value\',
  someH         


        
11条回答
  •  孤街浪徒
    2020-11-28 05:03

    In Rails 3, the View has a formats array, which means you can set it to look for [:mobile, :html]. Setting that will default to looking for :mobile templates, but fall back to :html templates. The effects of setting this will cascade down into inner partials.

    The best, but still flawed way, that I could find to set this was to put this line at the top of each full mobile template (but not partials).

    <% self.formats = [:mobile, :html] %>
    

    The flaw is that you have to add that line to multiple templates. If anyone knows a way to set this once, from application_controller.rb, I'd love to know it. Unfortunately, it doesn't work to add that line to your mobile layout, because the templates are rendered before the layout.

提交回复
热议问题