Rails Multipart Layouts and Yielding Sections

Search for a command to run...

No comments yet. Be the first to comment.
Rails 7 ships with a lot of really great improvements to the frontend the biggest of which is removing webpacker in favor of a more generic approach to allow developers to use whatever they'd like to use to compile their assets like esbuild or import...

While working on Paylooza it was really important to me to have a very well tested application. Paylooza strives to be an affordable, off-the-shelf recurring billing solution for individuals and small businesses and because we are dealing with transa...

With Rails 7 recently dropping there are a few improvements outside of the great new features that might take new projects by surprise. The first that I encountered after starting up a new Rails project was the bin/dev script that now uses foreman...

Hobby projects are great because they can expose you to problems you've never encountered before. While working on Scavenger Hunt Party! I was noticing some issues in the real-world that were not present in my development environment. The biggest o...

I'm subscribed to cassidoo's newsletter and one of the interesting things she always has in there is an Interview question of the week. Usually I don't spend too much time actually solving these but this one looked fun and I thought I had a pretty ...

app/views/layouts/application.html.erb.
<!DOCTYPE html> <html> <head> <title>Myapp</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <%= yield %> </body> </html>The highlighted line is the important one. That <%= yield %> tells Ruby on Rails that whatever view you load just drop it right there. That's great! Now we have a simple base layout that houses our imports and basic structure and CSS and we simply drop in our own content inside of this. Let's say we want to take it a step further. What if we want a left and a right side bar which is unique to each of our views? We just need to change our basic layout a little bit to add those columns and then update our view template code to take advantage of our new structure.
app/views/layouts/application.html.erb
<!DOCTYPE html> <html> <head> <title>Myapp</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <div id="left" class="sidebar"> <%= yield :left %> </div> <div id="content"> <%= yield %> </div> <div id="right" class="sidebar"> <%= yield :right %> </div> </body> </html>
app/views/MODEL/new.html.erb
<% content_for :left do %>
<h2>Left Side</h2>
<ul>
<li>...</li>
</ul>
<% end %>
<% content_for :right do %>
<h2>Right Side</h2>
<ul>
<li>...</li>
</ul>
<% end %>
<h1>Content Heading></h1>
Following the suggestion of Obie Fernandez and Kevin Faustino in their book The Rails 4 Waycontent_for will be dropped in that unnamed yield in the layout.
app/views/layouts/application.html.erb
<!DOCTYPE html> <html> <head> <title>Myapp</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <div id="left" class="sidebar"> <%= yield :left %> </div> <div id="content"> <%= yield %> </div> <div id="right" class="sidebar"> <%= render 'shared/right' %> </div> </body> </html>
app/views/shared/_right.html.erb
<h2>Right Side</h2> <ul> <li>...</li> </ul>
app/views/layouts/application.html.erb
<!DOCTYPE html> <html> <head> <title>Myapp</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <div id="left" class="sidebar"> <%= yield :left %> </div> <div id="content"> <%= yield %> </div> <div id="right" class="sidebar"> <%= render 'shared/right', user: user %> </div> </body> </html>
app/views/shared/_right.html.erb
<h2>Right Side</h2> <h3><%= user.name %></h3> <ul> <li>...</li> </ul>Pretty cool, right? I hope this introduction to layouts and partials was helpful. If there are any problems with my examples or if you have any questions please feel free to leave a comment below.