rabl snippets

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