render snippets

How to render a view or partial from a Rake task, model or anywhere

Tagged actionview, render, rake, view, rails, routes  Languages ruby

This is tested with Rails 3.0.7 and will most likely change in future versions of Rails:

# @newsletter in the view file
assigns = { :newsletter => self }

routes = Rails::Application.routes # Rails => YourApp
routes.default_url_options = { :host => 'xxx.com' }

av = ActionView::Base.new(Rails::Application::Configuration.new(Rails.root).paths.app.views.first, assigns)
av.class_eval do
  include ApplicationHelper
  include routes.url_helpers
end

self.html = av.render :template => 'newsletters/show'

After some experimentation I ended up with this code:

class RenderHelper
  class << self
    def render(assigns, options, request = {})
      request = { 
        "SERVER_PROTOCOL" => "http", 
        "REQUEST_URI" => "/",
        "SERVER_NAME" => "localhost", 
        "SERVER_PORT" => 80
      }.merge(request)

      av = ActionView::Base.new(ActionController::Base.view_paths, assigns)
      
      av.config = Rails.application.config.action_controller
      av.extend ApplicationController._helpers
      av.controller = ActionController::Base.new
      av.controller.request = ActionController::Request.new(request)
      av.controller.response = ActionController::Response.new
      av.controller.headers = Rack::Utils::HeaderHash.new

      av.class_eval do
        include Rails.application.routes.url_helpers
      end

      av.render options 
    end
  end
end

And this is how I use it:

RenderHelper.render(:newsletter => self, :template => 'mailchimp/daily')

Render Rabl Views Anywhere (views, jobs, mails)

Tagged rabl, json, render, helper  Languages ruby

A helper for rendering Rabl views anywhere, e.g. background jobs, views, mails, etc:

def render_rabl(locals, view, options = {})
  scope = Object.new
  locals.each do |key, value|
    scope.instance_variable_set :"@#{key}", value
  end
  options = { format: :json, scope: scope }.merge(options)
  Rabl.render(nil, view, options)
end

Usage:

locals = { users: User.all }
view = 'users/index'
render_rabl locals, view