ruby-on-rails-3 – 如何从Rails中的布局模板中获取当前视图名称?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 如何从Rails中的布局模板中获取当前视图名称?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以从布局中获取当前渲染视图的名称

解决方法

我为css命名空间做了类似的事情:
  1. # config/initializers/action_view.rb
  2. ActionView::TemplateRenderer.class_eval do
  3.  
  4. def render_template_with_tracking(template,layout_name = nil,locals = {})
  5. # with this gsub,we convert a file path /folder1/folder2/subfolder/filename.html.erb to subfolder-filename
  6. @view.instance_variable_set(:@_rendered_template,template.inspect.gsub(/(\..*)/,'').split('/')[-2..-1].join('-'))
  7. out = render_template_without_tracking(template,layout_name,locals)
  8. @view.instance_variable_set(:@_rendered_template,nil)
  9. return out
  10. end
  11.  
  12. alias_method_chain :render_template,:tracking
  13. end
  14.  
  15.  
  16. # application.html.erb
  17. <body class="<%= :@_rendered_template %>" >

猜你在找的Ruby相关文章