Skip to content

Instantly share code, notes, and snippets.

@basgys
Created December 12, 2012 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basgys/4266041 to your computer and use it in GitHub Desktop.
Save basgys/4266041 to your computer and use it in GitHub Desktop.
Rails - Render a partial in a different format
# Render a partial in a different format
#
# Parameter : String or Array
# Usage :
# <% with_format('html') do %>
# <%= render(partial: 'partial') %>
# <% end %>
def with_format(formats)
original_formats = self.formats
self.formats = formats.kind_of?(Array) ? formats : [formats]
result = yield
self.formats = original_formats
return result
end
# See http://stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment