The Rails Guides: Views and Controllers Flashcards
How does the Controller handle requests in Rails?
The Controller orchestrates the handling of requests in Rails, typically delegating heavy code execution to the Model. It then manages the response generation process, handing off to the View when it’s time to send a response back to the user.
What are the three ways to create an HTTP response from a controller in Rails?
From a controller, you can create an HTTP response by calling render to generate a full response, redirect_to to send a redirect status code, or head to create a response consisting solely of HTTP headers.
Explain the concept of “convention over configuration” in the context of default rendering in Rails.
“Convention over configuration” in Rails means that controllers automatically render views with names corresponding to valid routes by default, reducing the need for explicit rendering calls.
How does Rails automatically determine which view to render in a controller action?
Rails automatically renders a view with a name corresponding to the controller action unless explicitly specified otherwise. For example, the index action in the BooksController renders the index.html.erb view by default.
What happens if you don’t explicitly call render at the end of a controller action in Rails?
If you don’t explicitly call render at the end of a controller action in Rails, Rails will automatically look for the corresponding view template based on the action name.
What is the purpose of the render method in Rails controllers?
The render method in Rails controllers is used to render views, allowing controllers to generate content for use by a browser.
How can you render the view corresponding to a different template within the same controller?
You can render the view corresponding to a different template within the same controller by using the render method with the name of the view.
Can you render a template from an entirely different controller using the render method?
Yes, you can render a template from an entirely different controller using the render method by specifying the full path relative to app/views.
What are some options available for customizing the behavior of the render method?
Some options available for customizing the behavior of the render method include specifying content type, layout, location, status, formats, and variants.
What does the render :inline option do in Rails controllers?
The render :inline option in Rails controllers allows you to supply ERB directly as part of the method call.
When would you use the render plain: option in Rails?
The render plain: option in Rails is used to send plain text back to the browser, useful for responding to Ajax or web service requests.
How can you render an HTML string back to the browser in Rails?
You can render an HTML string back to the browser in Rails using the render html: option.
What is the purpose of rendering JSON in Rails, and how is it achieved?
Rails supports rendering JSON responses for use by Ajax libraries. This is achieved using the render json: option.
How does Rails support rendering XML responses?
Rails has built-in support for converting objects to XML and rendering that XML back to the caller using the render xml: option.
Can you render vanilla JavaScript using Rails? If so, how?
Yes, Rails can render vanilla JavaScript using the render js: option.
What does the render body: option do in Rails controllers?
The render body: option in Rails controllers sends a raw content back to the browser without setting any content type.
How can you render a raw file in Rails from an absolute path?
You can render a raw file in Rails from an absolute path using the render file: option.
What are the options available for specifying layouts in Rails?
Options available for specifying layouts in Rails include specifying layout files directly or deferring the choice of layout until runtime.
How can you defer the choice of layout until a request is processed in Rails?
You can defer the choice of layout until a request is processed in Rails by using a symbol or an inline method like a Proc.
What are conditional layouts in Rails, and how are they specified?
Conditional layouts in Rails are specified using the :only and :except options, which take method names corresponding to controller actions.
How does layout inheritance work in Rails controllers?
Layout inheritance in Rails controllers means that layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones.
How does template inheritance work in Rails controllers?
Template inheritance in Rails controllers means that if a template or partial is not found in the conventional path, the controller will look for a template or partial to render in its inheritance chain.
What can cause “double render errors” in Rails, and how can they be avoided?
“Double render errors” in Rails are caused by having more than one call to render or redirect in a single code path. They can be avoided by ensuring there’s only one call to render or redirect per action, possibly using return to exit early from the action.
What is the purpose of the redirect_to method in Rails?
The redirect_to method in Rails tells the browser to send a new request for a different URL.
How does redirect_back differ from redirect_to in Rails?
redirect_back returns the user to the page they just came from by pulling the location from the HTTP_REFERER header, while redirect_to sends the browser to a specified URL.
What happens to method execution after calling redirect_to or redirect_back in Rails?
Method execution continues after calling redirect_to or redirect_back in Rails. Statements occurring after them will be executed unless halted by an explicit return or some other halting mechanism.
How can you specify a different HTTP status code when using redirect_to in Rails?
You can specify a different HTTP status code, such as 301 for a permanent redirect, by using the :status option with the redirect_to method.
What misconception about redirect_to do inexperienced developers often have?
Inexperienced developers sometimes think of redirect_to as a “goto” command, moving execution from one place to another in Rails code. However, redirect_to actually stops the code execution and waits for a new request from the browser, sending back an HTTP 302 status code with the new request URL.
How can you handle scenarios where a required variable is nil using redirect_to in Rails?
If a required variable is nil, you can use redirect_to to redirect to another action instead of rendering. This ensures that the browser makes a fresh request and the necessary variables are set up properly.
What is the purpose of the head method in Rails?
The head method in Rails is used to send responses with only headers to the browser, allowing you to specify an HTTP status code and additional header options.
How can you use the head method to return an error header in Rails?
You can use the head method with a specific status code, such as :bad_request, to return an error header to the browser in Rails.
What are some scenarios where you might use partials in Rails views?
Partials in Rails views are useful for breaking down the rendering process into manageable chunks, reducing duplication, and organizing reusable pieces of code.
How do you pass local variables to partials in Rails?
Local variables can be passed to partials in Rails using the locals option with the render method or by using the as option when rendering a collection.