Web is all about communication, and Rails is one of the web frameworks that make it possible. You write a URL in the address bar in your web browser, and press enter; a request is generated that comes back with a response after some time. Most of the time the response is HTML, but there are also exceptions to this rule, like response may be some data in JSON or XML format, or it could be the plain text like success word. Let’s see how does Rails take a request, does processing on it and returns back a response.

When a request is made to Rails framework, the very first thing it hits is the routes file located at config/routes.rb, and it is up to your routes file to decide which controller, and which action within that controller to forward that request to. Let’s take a very general example:

get 'users/new' => 'users#new'

In the example, whenever one tries to access localhost:3000/users/new, the new action within UsersController will be executed. Good news is that Rails is intelligent enough that you can even skip the second part, and can write only the following line:

get 'users/new'

And Rails will automatically infer that when this URL gets hit, new action in UsersController is to be invoked.

Generally, when you make a request through a browser’s address bar, it is an HTML request, but that is not the only kind of request. When you use remote: true or data-remote=true in any form elements, your request becomes a JS request, and Rails takes it differently than an HTML request. Another format: JSON, you can have this format if you use AJAX and you specify dataType: ‘json or you append .json at the end of URL in the browser.

After the decision of controller file, and action within that controller file, it is time to respond back with data.

Let’s start with the simplest case first:

def show
 @user = User.find_by_id(params[:id])
end

If this method is invoked by the request, Rails will look for a file called show.html.erb in path app/views/users/ and if it doesn’t find one, it will throw an exception saying ActionView::MissingTemplate. You can change this behavior by using render method like render ‘another_file’, but bare in mind that this is valid only if your request is of type HTML. If your request hits your server with JS or JSON format, the things change quite a lot.

If a request hits with the format of JSON, it is up to your controller to respond back in the said format, and if the controller fails to do so, the exception ActionView::MissingTemplate gets generated. So how do you respond back with a JSON format?

In order to respond back in formats other than HTML, we use a method called respond_to that takes a block in which we can specify different things for different formats. Here’s how:

respond_to do |format|
 format.html { render html: '<h1>Just a headline</h1>'.html_safe } 
 format.json { render json: 'success' }
end

So this method now doesn’t make the request go for looking a file of the same name as method’s, rather it simply returns the inline HTML, in our case ‘<h1>Just a head line</h1>’.html_safe. Calling html_safe to not to escape the HTML entities.

Same way, if a JSON request comes in, only the response ‘success’ will be returned. On the other hand, if you want to return response from a file, you would have to do something like:

respond_to do |format| 
 format.html 
 format.json
end

This will simply make sure that your method responses back in both formats: html as well as json, but to return the actual response, your controller would look for the files show.html.erb and show.json.jbuilder for the respective requests.

So this was a short yet comprehensive tutorial for anybody who wants to understand how do Rails generate a response from a request. For more information, you may head over to Ruby on Rails official guides at this link. Guides cover a lot more than what we have shown in this article. So, that’s it for this article, see you next time. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *