Rails应用中控制器的主体类 [英] Body class for controller in Rails app
本文介绍了Rails应用中控制器的主体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我的布局中有这个
<body class="<%= controller.controller_name %>">
我想添加一个附加类,该类对于设置了它的任何控制器中的所有动作都是相同的,例如:
I want to add an additional class that will be the same for all actions in any controller where it's set, something like:
class SomeController < ApplicationController
body_class 'page'
...
end
class AnotherController < ApplicationController
body_class 'page'
...
end
这将导致:
<body class="some page">
<body class="another page">
最简单的方法是什么?我可以为此使用控制器类变量吗?
What would be the easiest way to achieve this? Can I use controller class variables for this?
推荐答案
我的解决方案:
控制器:
class SomeController < ApplicationController
before_filter lambda { @body_class = 'page' }
...
end
布局:
<body class="<%= "#{controller.controller_name} #{@body_class}".strip %>">
这篇关于Rails应用中控制器的主体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文