DEV Community

JackRB52
JackRB52

Posted on

Simplification and Ruby Magic

Intro

Diving back into Ruby on Rails after about 6 months, it took me a little time to start to regain my comfort with the flow between route, control, action, and view. As I've re-acquainted myself with the basics of coding in Ruby, I've also started to learn the beauty of coding RESTfully. This has allowed me to start shrinking the amount of code needed, and understand how to use "Ruby Magic" to make my code simpler and more functional.

Simple Code

Two of my favorite code simplifications have been using a simpler resources command to generate the standard routes and route labels, and the form_with :model helper. The route helper ensures I have RESTful routes and route aliases ready to go. And the form helper capitalizes on the RESTful code, and makes further assumptions, to simplify data collection. The helper relies on RESTful routes and aliases to know to call helper methods for routes to generate the form url. And uses the object being used to identify whether it is creating new data or updating existing data. Once it has that information, the helper makes storing related attributes together in one hash simple, so that mass assignment is possible.

Searching for Answers

The other thing I've noticed is that learning how to simplify code, from new ways of writing a hash, to relying more on helpers and available methods has enabled me to begin to understand more code online. When looking for helpful guidance on how to finish the code, I was able to recognize when a simpler hash or a helper was being used, and why tags and information I had previously included were not needed because of the RESTful code assumption.

Top comments (0)