每个控制器的 Rails 布局 [英] Rails layout per controller

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

问题描述

我有一个家庭控制器和一个新闻控制器.我希望这两个控制器都使用 application.html.erb 布局文件,除此之外,对于主页,使用主页布局和新闻,使用新闻布局.然后渲染一个特定的视图.这在 Rails 中可行吗?

I have a home controller and a news controller. I want both of these controller to use the application.html.erb layout file and in addition to that, for home, use the home layout and news, use the news layout. and then render a specific view. Is this possible in rails?

换句话说,我不想为每个视图指定一个布局,而是为每个控制器指定一个布局,两者都继承自 application.html.erb 布局.

In other words, I don't want to specify a layout per view, but per controller, both inheriting from application.html.erb layout.

我想要做的是删除添加顶部导航栏和在每个布局文件中包含 javascript/css 的冗余.我宁愿将其包含在一个文件中,并使用另一个布局进行控制器特定布局,然后最终呈现视图.

What i want to do is remove the redundancy of adding the top navigation bar and including javascript/css in every single layout file. I'd rather include that in one file, and do controller specific layout with another layout, and then finally render the view.

谢谢

推荐答案

你可以告诉控制器使用特定的布局,例如

You can tell a controller to use a specific layout, eg

class HomeController < ApplicationController
  layout 'home'
end

class NewsController < ApplicationController
  layout 'news'
end

这些将期望 app/views/layouts/home.html.erb 等中的布局

These will expect the layouts in app/views/layouts/home.html.erb etc

这篇关于每个控制器的 Rails 布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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