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
yieldthing 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_tagin theheadelement.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
yieldmethod 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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access