Skip to content

Render Thredded partials outside of Thredded

Gleb Mazovetskiy edited this page Oct 11, 2017 · 12 revisions

First, whitelist the parts of rack.env required to identify the current user in ActionController::Renderer (more information). If you use Devise, the key that you need to whitelist is warden:

# config/initializers/application_controller_renderer.rb
ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'

Now you can render any Thredded partial using Thredded::ApplicationController.renderer. For example:

<%= Thredded::ApplicationController.renderer.new(request.env.slice('warden'))
      .render(partial: 'thredded/shared/nav') %>

If you do it in several places, define a helper method:

# Render in the context of the Thredded engine
# @return [String]
def thredded_render(*args, **kwargs, &block)
  @_thredded_renderer ||= Thredded::ApplicationController.renderer.new(request.env.slice('warden'))
  @_thredded_renderer.render(*args, **kwargs, &block)
end

Folks on rails 4 need to include the backport_new_renderer for this method to work.