What is the standard way to add a heade开发者_C百科r and footer to every view in a Rails app?
If this file is found, it will be used.
app/views/layouts/application.html.erb
<!doctype html>
<html>
<head>
<!-- stuff -->
</head>
<body>
<!-- this is where content will be rendered -->
<%= yield %>
</body>
</html>
Otherwise, you can call in a different one.
# app/controllers/birds_controller.rb
class BirdsController < ApplicationController
layout :birds # looks for app/views/layouts/birds.html.erb
# ...
end
put the header and footer into the application layout which can be found at app/views/layouts/application.html.erb.You may have to create this file first.
To add any "boilerplate" code to all pages, use a layout file. It is usually found in app/views/layouts/.
Create the page as you would with any other Rails view. In general, it is a good idea to place the <html>
, body
, etc tags inside of the layout to avoid repetition.
In the location where you want the content from individual views to appear, put in a <% yield %>
tag. Because of Ruby's block syntax and the way Rails implements layouts, this will allow any view whose controller specifies this layout to "inherit" all of the layout and insert only the page-specific content.
To use the layout globally, name the file application.html.erb or specify the render :layout option.
You'll find your app's layout files in app/views/layouts/
.
create a common layout 'app/views/layouts/.html.erb' as x1a4 said. And inside that you can create your header and footer
If you want you can make that as two partials and call inside the layout file. But if you have only one layout you might not need this
having header and footer in partials make sense if you have 2-3 layout types (like for normal users, administrators etc...)
and in your controllers right after the class declaration
class UsersController < ApplicationController layout 'layout name' end
cheers, sameera
精彩评论