Rails指定静态页面的布局 [英] Rails specify layout for static pages

查看:66
本文介绍了Rails指定静态页面的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有一些静态内容:

My application has some static content:

root :to => 'pages#home'
match '/about',   :to => 'pages#about'
match '/contact', :to => 'pages#contact'
match '/help',    :to => 'pages#help'

我希望这些页面共享布局,而不是在每个文件中重复页眉/页脚.

I want these pages to share a layout rather than duplicate the header / footer in each file.

我发现的有关布局的唯一文档似乎表明我应该在控制器级别指定布局,对吗?

The only documentation around layouts I have found seems to indicate that I should specify my layouts at the controller level, is that correct?

如果是这样,应该如何给我的静态页面布局?我看到的唯一变通方法是为每个页面创建一堆空控制器,其唯一目的是指定一个通用布局文件,但是闻起来有点过头了.

If so, how should give my static pages layouts? The only workaround I see is to create a bunch of empty controllers for each page, for the sole purpose of specifying a common layout file, but that smells like overkill.

我错过了一个把戏吗?

感谢您的帮助

推荐答案

是的,在控制器级别设置布局.您可以为这组页面创建一个控制器,如下所示:

Yes, set the layout at the controller level. You could create a controller for this group of pages like this:

class AboutController < ApplicationController
    layout "about"

    def about
    end

    def contact
    end

    def help
    end
end

对于这种事情,我倾向于使用名称"AboutController",以避免与根本不通过控制器的真正静态文件混淆.但是您可以随便命名.

I tend to use the name "AboutController" for this sort of thing, to avoid confusion with truly static files that don't pass through a controller at all. But you could name it whatever you like.

将您的路线指向它(例如about#contact).然后在app/views/layouts/about.html.erb

Point your routes at it (e.g. about#contact). Then create the layout in app/views/layouts/about.html.erb

如果需要更改特定动作的布局,则可以使用render方法的:layout选项:

If you ever need to change the layout for a particular action, you can use the render method's :layout option:

def something_special
    render :layout => "other"
end

如果您根本不需要布局,例如robots.txt,也可以传递false.

You can also pass false if you don't want a layout at all, like for robots.txt.

这篇关于Rails指定静态页面的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆