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

前端 未结 11 1256
执念已碎
执念已碎 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 04:55

    Just elaborating on what zgchurch wrote:

    • taking exceptions into account
    • returning the result of the called block

    Thought it might be useful.

    def with_format(format, &block)
      old_formats = formats
      begin
        self.formats = [format]
        return block.call
      ensure
        self.formats = old_formats
      end
    end
    

提交回复
热议问题