This post is going to be very usefull for beginners, We're going to talk about Rails layouts in the post.
Rails layout make it easy to create consistent templates for your web application.
If you're familiar with reach components then it's the same as creating a reusable components for your app.
If you're building a Ruby on Rails app, you'll often need to reuse the same structure, like a header, footer, or navigation bar, across multiple pages Right.
This is where Rails layouts come in play.
In this post, I’ll explain how to work with layouts in Rails, share practical examples, and show you why they’re a powerful tool for reusable templates.
How to Create and Use Layouts in Rails
Rails comes with a default layout file called application.html.erb, located in the app/views/layouts directory. This layout is automatically applied to all views unless you specify a different one.
1. The Default Layout: *application.html.erb*
By default, Rails uses the application.html.erb
layout. Here's an example of what it might look like:
erb
<!DOCTYPE html>
<html>
<head>
<title>MyRailsApp</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<header>
<%= render 'shared/header' %>
</header>
<main>
<%= yield %>
</main>
<footer>
<%= render 'shared/footer' %>
</footer>
</body>
</html>
Here <%= yield %>
is a placeholder for your view content if you don't know. Rails will replace this with the specific view you’re rendering.
Shared components like headers and footers can be rendered as partials to keep your layout organized.
2. Specifying a Different Layout
You can specify a custom layout for specific controllers or actions. For example:
ruby
class AdminController < ApplicationController
layout "admin"
end
In this case, Rails will look for a file named admin.html.erb
in the app/views/layouts
directory. This is useful if you want a different design for your admin panel compared to the main site.
Adding Partials to Layouts
Partials are reusable snippets of HTML and Rails code. They make your layouts cleaner by breaking them into smaller, manageable pieces. For example, you might create a partial for a navigation bar:
1. Create a partial in app/views/shared/_navbar.html.erb
:
erb
<nav>
<ul>
<li><%= link_to 'Home', root_path %></li>
<li><%= link_to 'About', about_path %></li>
</ul>
</nav>
2. Render it in your layout:
erb
<%= render 'shared/navbar' %>
Using Layouts for Specific Views or Pages
Sometimes, you want a layout to apply only to certain pages. For example, you might have a marketing layout for your landing pages. You can specify a layout for individual actions using the render
method:
ruby
def landing_page
render layout: "marketing"
end
This will use the marketing.html.erb
layout only for the landing_page
action.
Dynamic Content in Layouts
Rails layouts support dynamic content using instance variables or helper methods. For example, you can add a dynamic title:
1. In your layout (application.html.erb
), include a title placeholder:
erb
<title><%= content_for?(:title) ? yield(:title) : "Default Title" %></title>
2. In your view, set the title:
erb
<% content_for :title, "Custom Page Title" %>
This allows you to have unique titles for each page while keeping the layout reusable.
Rails layouts make your app consistent by giving all pages the same structure, like a shared header and footer. They are reusable, so you only need to set up the structure once and use it across multiple pages.
This also makes maintenance easier—any updates to the layout automatically apply to all pages. Plus, layouts are flexible, allowing you to create different designs for various sections, like a main site and an admin panel.
Rails layouts are a simple yet powerful way to manage reusable templates in your application.
They save time, keep your code organized, and ensure a consistent user experience. By combining layouts with partials and dynamic content, you can build flexible and maintainable templates for any Rails app.
Whether you’re building a small project or a complex application, mastering layouts is an essential skill that will make your Rails development smoother and more enjoyable. Try it out in your next project—you’ll appreciate the difference it makes!