Rendering views outside of controllers in Rails 5

Prathamesh Sonpatki

By Prathamesh Sonpatki

on January 8, 2016

This blog is part of our  Rails 5 series.

Rails request-response cycle is very easy to understand. A request hits the app, a route is matched to a controller action from routes.rb, and finally controller action processes the request and renders HTML or JSON based on the type of the request.

But sometimes we want to render our HTML or JSON response outside of this request-response cycle.

For example let's say user is allowed to download PDF version of a report on web. This can be done using request-response cycle. We also need to send a weekly report to managers and the email should have the report as an attachment. Now we need to generate the same PDF but since emails are sent using background job the request-response cycle is missing.

Rails 5 has this feature baked in.

Let's say we have a OrdersController and we want to render individual order outside of controller.

Fire up rails console and execute following command.

1
2OrdersController.render :show, assigns: { order: Order.last }
3

This will render app/views/orders/show.html.erb with @order set to Order.last. Instance variables can be set using assigns in the same way we use them in controller actions. Those instance variables will be passed to the view that is going to be rendered.

Rendering partials is also possible.

1
2OrdersController.render :_form, locals: { order: Order.last }
3

This will render app/views/orders/_form.html.erb and will pass order as local variable.

Say I want to render all orders, but in JSON format.

1
2OrdersController.render json: Order.all
3# => "[{"id":1, "name":"The Well-Grounded Rubyist", "author":"David A. Black"},
4       {"id":2, "name":"Remote: Office not required", "author":"David & Jason"}]
5

Even rendering simple text is possible.

1
2>> BooksController.render plain: 'this is awesome!'
3  Rendered text template (0.0ms)
4# => "this is awesome!"
5

Similar to text, we can also use render file and render template.

Request environment

A typical web request carries its own environment with it. We usually handle this environment using request.env in controllers. Certain gems like devise depends on env hash for information such as warden token.

So when we are rendering outside of controller, we need to make sure that the rendering happens with correct environment.

Rails provides a default rack environment for this purpose. The default options used can be accessed through renderer.defaults.

1
2>> OrdersController.renderer.defaults
3=> {:http_host=>"example.org", :https=>false, :method=>"get", :script_name=>"", :input=>""}
4

Internally, Rails will build a new Rack environment based on these options.

Customizing the environment

We can customize environment using method renderer. Let's say that we need "method as post" and we want "https to be true" for our background job processing.

1
2renderer = ApplicationController.renderer.new(method: 'post', https: true)
3# => #<ActionController::Renderer:0x007fdf34453f10 @controller=ApplicationController, @defaults={:http_host=>"example.org", :https=>false, :method=>"get", :script_name=>"", :input=>""}, @env={"HTTP_HOST"=>"example.org", "HTTPS"=>"on", "REQUEST_METHOD"=>"POST", "SCRIPT_NAME"=>"", "rack.input"=>""}>
4

Now that we have our custom renderer we can use it to generate view.

1
2renderer.render template: 'show', locals: { order: Order.last }
3

Overall this is a nice feature which enables reuse of existing code.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.