Using Templates
Explore how to create dynamic web pages in Rails using templates stored in app/views. Understand the template environment, controller data access, and different template types like ERB and Jbuilder. Learn how templates work with controllers to produce dynamic and interactive content.
We'll cover the following...
When we write a view, we’re writing a template: something that will get expanded to generate the final result. To understand how these templates work, we need to look at three things:
- Where the templates go
- The environment they run in
- What goes inside them
Where do the templates go
The render() method expects to find templates in the app/views directory of the current application. Within this directory, the convention is to have a separate subdirectory for the views of each controller. Our Depot application, for instance, includes products and store controllers. As a result, our application has templates in app/views/products and app/views/store. Each directory typically contains ...