Rails How To Include Existing Layouts To Another Layout

Do you like this?

Summary:
When I first learned Rails, it was hard to find this since different terminologies were used. Hence, I decided to blog about this just in case other developers with Java or Django backgrounds are searching for the same thing, this can become useful.


Content:

In Rails world, this is called partial layouts. For example, you want to separate header content with the page content. In your index.html.erb, you want to include the header page file. This can be done by naming the file _header.html.erb in the same view folder such as app/views/<your_model>/_header.html.erb. And in your index.html.erb, you need to include this line:

<%= render "header" %>
One problem with this is the header file might be used across your application. The solution to this is to put the file under another folder such as shared folder and then include this line in your index.html.erb:
<%= render "shared/header" %>

 
comments powered by Disqus