Views and Models
Explore how Rails views use templates, helpers, and assets to render dynamic content while models manage data and business logic through the database. Understand the role of views and models within Rails architecture to create maintainable applications.
We'll cover the following...
Views
Rails support for rendering HTML web views is quite sophisticated and powerful. In particular, the coupling between the Active Model and Rails’ form helpers is very tight (a great example of the power in tightly coupling components). Actions performed by boundary classes that result in dynamic output (usually controllers and mailers) will initiate the rendering of the view from a template, and that template may pull in JavaScript, CSS, or other templates (partials).
Often, the templates are HTML, but they can be pretty much anything, including JSON, text, or XML. Templates also have access to helpers, which are free functions in the global namespace. Rails provides many helpers by default, and we can make our own.
View code tends to feel messy because while a particular template can be isolated pretty well, including decomposing it into reusable partials, CSS and JavaScript, by their nature, aren’t organized in the same way. Often, CSS and JavaScript are globally available, and taking care to keep them isolated can be tricky.
Rails 7 includes what