Test Your Knowledge
Quiz
Where would you put your CSS stylesheet, and how should you connect it to your view?
How does Rails know which layout to apply to a particular view?
What does that
yield
thing do?How do I send data from the view template to the layout?
Answers
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 thehead
element.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.
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.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.