When you start your first Rails application you might think that are only capable of rendering HTML. So why and how to use a block? Here, we will show you how handy blocks can be. controller actions respond_to respond_to The first step is to create a new project and generate a in Rails. Let’s take a look at some of our controller actions. scaffold def index @blogs = Blog.allend As you can see, action does not have a block. So, this will work when just rendering an HTML. But what if, however, a client asks to get the page in TEXT format? It will lead to an : index respond_to exception ActionView::MissingTemplate (Missing template blogs/index ... with { ... :formats=>[:text], ...}) Rails goes through the registered formats and tries to find a compatible format for the . If there is no handler it will raise an error. Credits to . MIME type in the request max Instead of telling our clients that the , we want to tell them that the . You could add a block to your action: file is missing requesting format is not supported respond_to index def index @blogs = Blog.all respond_to do |format| format.html # index.html format.js # index.js format.xml { render xml: @blogs } endend After the change, the client will get when the format is not supported. Also, your action will respond to two new formats: js and xml. 406 error index If you need a simple and quick way to render your objects, you can take this , it works in the same way as the example above: shortcut def index @blogs = Blog.all respond_to :html, :json, :xmlend Not sure if this really works? Go on and test it using and ! RSpec FactoryBot It’d be nice if you could have a that would affect the controller. Usually, each action in your controller can work with the same formats, you can achieve this by using . Here is an example of how to implement it: respond_to entire respond_with class BlogController < ApplicationController respond_to :html, :json, :xml # GET /blogs # GET /blogs.json # GET /blogs.xml def inde @blogs = Blog.all respond_with(@blogs) endend If you need more control and want to be able to have a few actions that act differently, you can always use a full respond_to block. In Rails 4.2, is no longer included. But you can get it back if you . Once you install it and generate a , the generator will create controllers using instead of . respond_with install the responder gem Rails scaffold respond_with respond_to class BlogController < ApplicationController before_action :set_blog, only: [:show, :edit, :update, :destroy] respond_to :html, :json def index @blogs = Blog.all respond_with(@blogs) end ...end Format ALL or Format ANY? If you’d like to specify a to render something for all formats while keeping other options untouched, you can always use in the following way: respond_to format.all respond_to do |format| format.csv { render_csv } format.all { render_404 }end Use if you want to specify a common block of different formats: format.any def index @blogs = Blog.all respond_to do |format| format.html format.any(:xml, :json) { render request.format.to_sym @blogs } endend All the need to be tested to make sure they work as intended! controller actions Defining custom MIME type If you need to use a MIME type which isn’t supported by default, you can register your own handlers in : config/initializers/mime_types.rb Mime::Type.register "text/markdown", :markdown That’s it for this article! Thank you for taking the time to read it! Originally published at kolosek.com on March 5, 2018.