Test Your Knowledge

Quiz

  1. Where would you put your CSS stylesheet, and how should you connect it to your view?

  2. How does Rails know which layout to apply to a particular view?

  3. What does that yield thing do?

  4. How do I send data from the view template to the layout?

Answers

  1. Stylesheets go in the public/stylesheets directory, and you connect them to your views (or layouts) by putting a call to stylesheet_link_tag in the head element.

  2. By default, Rails will apply the layout in app/views/layout/application.html.erb to all of your views. However, if there is a layout file in app/views/layout/ that has the same name as a view, Rails will use that instead.

  3. The yield method hands control to a different block of code, one that was passed with parameters. Rails often handles this quietly, making it easy to share data between, for example, layouts and views.

  4. The layout has access to all of the same variables the view uses. You don’t need to do anything special to pass variables to the layout, even if you want the layout to apply them early in your HTML document.

Get Learning Rails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.