Get an improved navigation experience with a Chrominium based browser.
Dismiss
Ruby Video
Talks
Speakers
Events
Leaderboard
Sign in
Talks
Speakers
Events
Use
Analytics
Sign in
Suggest modification to this talk
Title
Description
Building a Rails Controller From Scratch by Alex Kitchens If you replaced ActionController with an implementation of your own, what would you have to build to get your app working again? In this talk, we'll do just that. We'll see how the controller interacts with the router to receive a request, process it, and return a response. We'll also rebuild controller features like Params, Controller Callbacks, and Rendering. In the end, we'll have a new functioning controller class called AwesomeController, and we will have seen what it takes to process a request in Rails. __________ Alex Kitchens (@alexcameron89) is a software engineer at Stitch Fix, where he gets to embrace his love of Rails as a backend developer.
Date
Summary
Markdown supported
In the talk "Building a Rails Controller From Scratch" at RailsConf 2020 CE, Alex Kitchens demonstrates how to create a functioning Rails controller without inheriting from ActionController. The presentation walks through replacing ActionController with the speaker's own implementations, dubbed AwesomeControllerBase and AwesomeControllerAPI. The goal is to uncover the internal workings of a controller by mimicking the core functionalities of ActionController, providing a clearer understanding of how Rails processes requests and responses. ### Key Points: - **Purpose of the Talk**: Alex emphasizes his interest in understanding how controllers work in Rails, beyond just writing actions, by building a controller from scratch. - **Initial Setup**: The project starts with a simple Rails blog with two controllers for posts—one for web and the other for API—with tests confirming their correct behavior. - **Implementing Dispatch Logic**: The primary method needed is `dispatch`, which the router calls to provide the action name, request object, and a response object. Alex details how to correctly handle this method in order to facilitate the flow from request to response. - **Handling Parameters**: To manage request parameters, Alex introduces a modular approach for implementing necessary functionalities such as `params`, `require`, and `permit`, ensuring that the controller can access and process the parameters correctly. - **Rendering Responses**: The presentation also covers the implementation of the `render` method for both API and web responses. The API version is comparatively straightforward, while the web controller requires implicit rendering to handle HTML templates. - **Controller Callbacks**: Alex explains the importance of implementing controller callbacks (before, after, and around actions) to handle operations that should occur in relation to specific actions, enhancing the controller's capabilities. - **Conclusion**: By the end of the talk, Alex summarizes the outcomes of creating these minimal controller classes, underlining the importance of understanding the interaction between requests and responses in the Rails lifecycle. He emphasizes that while the resultant code is not production-ready, it serves as a great educational tool to grasp the concepts behind controllers. ### Takeaways: - Building basic components of a Rails controller from scratch allows for a deeper understanding of how Rails operates under the hood. - Implementing essential features such as dispatch, rendering, and parameter handling can significantly demystify Rails' controller functionality. - Encouragement to explore the ActionController code in Rails for further learning opportunities.
Suggest modifications
Cancel