简体   繁体   中英

How can I render a partial view from within a layout file?

To add some context, I learned that within a view folder you can just create a file and add a partial with "_partialname", such as "_login". on my index.html.erb file or any other file I would usually place a:

<%= render "login" %>

Which is what I did for the layouts folder, but it is not working.

Below is a snippet of my file /views/layouts/application.html.erb . Within the same layouts folder, I created a _footer.html.erb , which I want all my views to have, but when I try the following, it gives me an error:

</head>

<body>
<%= yield %>

<%= render "footer" %>
 </body>
</html>

Adding error message:

Missing partial static/_footer, application/_footer with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. 
Searched in:
 * "/Users/rogerperez/DevProjects/Property-Pal/app/views"

Update: added context on for the reason on my question

I believe your path is wrong in this case. Change the code to

<%= render 'layouts/footer' %>

该代码将从app / views / layouts / _footer.html.erb中提取部分代码。

<%= render :partial => "layouts/footer" %>

如果_footer.hml.erb在layouts文件夹中,则可以通过以下方式在../views/layouts/application.html.erb中进行渲染:

<%= render 'layouts/footer' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM