Using Templates

Get deep insights into Ruby templates

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 templates named after the actions in the corresponding controller. We can also have templates that aren’t named after actions. We render such templates from the controller using calls such as these:

Get hands-on with 1200+ tech skills courses.