在 Ruby on Rails Web 应用程序中插入页眉和页脚?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15308604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting Headers and Footers in Ruby on Rails web application?
提问by hellomello
I'm coming from PHP development and I'm trying to learn Ruby on Rails. I'd like to know what is the best practice for inserting headers and footers? Typically in PHP i'd just do include('header.php');and then include('footer.php');
我来自 PHP 开发,我正在尝试学习 Ruby on Rails。我想知道插入页眉和页脚的最佳做法是什么?通常在 PHP 中我会做include('header.php');然后include('footer.php');
Now I'm trying to learn Ruby on Rails and trying to understand how or where I should put these header/footer files?
现在我正在尝试学习 Ruby on Rails 并试图了解我应该如何或在哪里放置这些页眉/页脚文件?
I created a new application
我创建了一个新应用程序
rails new new_app
Then I generated a new controller
然后我生成了一个新的控制器
rails generate controller SignUp
This created some files and folders. I've developed some HTML inside the new new_app/views/sign_upfolder, but I'd like to include header and footer to this page and for future pages. Where should I have these files? In the same folder? or under the default folder in new_app/views/layouts? Also, how do I even include files once I created them?
这创建了一些文件和文件夹。我已经在新new_app/views/sign_up文件夹中开发了一些 HTML ,但我想在这个页面和以后的页面中包含页眉和页脚。我应该在哪里拥有这些文件?在同一个文件夹?或在默认文件夹下new_app/views/layouts?另外,一旦我创建了文件,我该如何包含它们?
I'm new to ruby on rails development and I'd like to gain some knowledge from experts! Thanks!
我是 ruby on rails 开发的新手,我想从专家那里获得一些知识!谢谢!
回答by Dev R
In the new_app/views/layoutsthere will be a file called application.html.erb. Open that file and put your header content above where it is written <%= yield>and footer content below <%=yield>.
在new_app/views/layouts里面会有一个名为application.html.erb. 打开该文件并将您的页眉内容放在写入位置的上方,将<%= yield>页脚内容放在下方<%=yield>。
I usually make a parital in layouts file called _header.html.erband _footer.html.erband do something like this :-
我通常在名为_header.html.erband 的布局文件中创建一个 parital_footer.html.erb并执行以下操作:-
<%= render "layouts/header" %>
<%=yield %>
<%= render "layouts/footer" %>
回答by Srikanth Venugopalan
You could create a partial for header and footer, and include them using renderin the layout file, if you have dynamic content. Else you could just edit your layout file to have the header and footer.
render如果您有动态内容,您可以为页眉和页脚创建部分,并将它们包含在布局文件中。否则,您只需编辑布局文件即可获得页眉和页脚。
Your scaffold generated views for SignUp, will all use the layout file to render the final HTML, so everything in the app/views/layouts/application.html.erbfile will get included in the output (assuming that you are using this file as the layout file for your view).
您为 SignUp 生成的脚手架视图将全部使用布局文件来呈现最终的 HTML,因此app/views/layouts/application.html.erb文件中的所有内容都将包含在输出中(假设您使用此文件作为视图的布局文件)。

