-
Notifications
You must be signed in to change notification settings - Fork 51
Render object directly
There are cases when you want to render object outside Rails view context. For instance to render objects in the console or to create message queue payloads. For these situations, you can use RablRails.render
as show below:
RablRails.render(object, template, :view_path => 'app/views', :format => :json) #=> "{...}"
You can specify the format
you want the object rendered (default to JSON) and also the view_path
where views should be looked for (default is Rails default, 'app/views').
You can also passed locals
in the options. You can define the object to render with the keyword object
. For example, this will output exactly the same as the code above:
RablRails.render(nil, template, :view_path => 'app/views', :format => :json, :locals => { :object => object })
But locals are really useful when you're using instance variables in your templates. For instance, if you have the following template
object :@post
node(:read) { |p| p.read_by?(@user) }
you can set the @user
instance variable in the locals options:
RablRails.render(post, template, :locals => { :user => user })
If you need to include view helpers or custom helpers, you'll need to include them in the rendering context
RablRails::Renderer::ViewContext.send(:include, Rails.application.routes.url_helpers)