Rails Layout name inside view

对着背影说爱祢 提交于 2020-01-30 05:16:11

问题


How can I print out the name of current layout in a view?

Example

puts controller.current_layout

Thx


回答1:


This works with Rails 3.0.7:

controller.send :_layout

Obviously, it's a private method, so use at your own risk.




回答2:


This works for me: response.layout

UPDATE: True, response.layout does not work in rails3+ thus I usually define a @layout_name variable inside each layout.

Example of application.html.haml

- @layout_name = 'application'
!!! Strict
%html
...



回答3:


<%= controller.active_layout %> gets you the layout file name. So layout 'application' would return layouts/application.html.erb

In Rails 2.1.0 and greater.



来源:https://stackoverflow.com/questions/2622219/rails-layout-name-inside-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!