Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

ruby on rails - How to call a .html.erb file inside another .html.erb file

I have a structure which looks like below where-in I need to call a .html.erb file in another.

main_layout.html.erb

<html>
    <head>
    </head>
    <body>
      <div>
        how to call the sub_layout here
      </div>
    </body>
</html>

sub_layout.html.erb

<div>
  <p> This is the nested layout to be rendered inside main_layout </p>
</div>

I tried with the below options.

<%= render sub_layout.html.erb %>
<%= ERB.new(open("sub_layout.html.erb").read).result(binding) %>

Both of the above options is not working. Any suggestions please?

question from:https://stackoverflow.com/questions/65844772/how-to-call-a-html-erb-file-inside-another-html-erb-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
# main_layout.html.erb
<html>
    <head>
    </head>
    <body>
      <div>
        <%= render 'sub_layout' %>
      </div>
    </body>
</html>

This will look for a file _sub_layout.html.erb in the same folder as main_layout.html.erb

If the _sub_layout.html.erb is not in the same folder as main_layouts.html.erb you need to define the complete path <%= render 'your/path/to/sub_layout' %>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...