Ruby rails: layouts
From wikinotes
The topmost layer loaded into views.
Contains html/header/body tags, sources stylesheet, view gets rendered within here.
If a layout whose name matches your controller exists, it is used by default.
Otherwise, the application.html.erb
layout is used.
You can also manually set the layout on the controller.
Documentation
Layouts/Rendering docs https://guides.rubyonrails.org/layouts_and_rendering.html
Locations
{project}/app/views/layouts/application.html.erb
layout applied to all views, unless a more specific layout is available. {project}/app/views/layouts/mailer.html.erb
layout applied to all emails, unless a more specific layout is available. {project}/app/views/layouts/{controller}.html.erb
controller-specific layout.
Example
<!DOCTYPE html> <html> <head> <title>RailsGuestbook</title> <%= csrf_meta_tags %> <!-- prevent cross-site request forgery --> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <%= yield %> <!-- view rendered here --> </body> </html>
Variables/Scope
Through ruby eruby, layouts have access to all of the same variables as their views.
- methods/attrs from ApplicationController (shared by all)
- methods/attrs from matching Controller
In addition to others
- text recorded in view using
<% content_for(...) do %>