How to get current view name from layout template in Rails?

Is it possible to get the name of the current rendered view from the internal layout?

+8
ruby-on-rails-3
source share
6 answers

I did something like this for css namespacing:

# config/initializers/action_view.rb ActionView::TemplateRenderer.class_eval do def render_template_with_tracking(template, layout_name = nil, locals = {}) # with this gsub, we convert a file path /folder1/folder2/subfolder/filename.html.erb to subfolder-filename @view.instance_variable_set(:@_rendered_template, template.inspect.gsub(/(\..*)/, '').split('/')[-2..-1].join('-')) out = render_template_without_tracking(template, layout_name, locals) @view.instance_variable_set(:@_rendered_template, nil) return out end alias_method_chain :render_template, :tracking end # application.html.erb <body class="<%= :@_rendered_template %>" > 
+4
source share

The active_template_virtual_path method of the method returns the template as a name in the following form: "controller / action"

  class ActionController::Base attr_accessor :active_template def active_template_virtual_path self.active_template.virtual_path if self.active_template end end class ActionView::TemplateRenderer alias_method :_render_template_original, :render_template def render_template(template, layout_name = nil, locals = {}) @view.controller.active_template = template if @view.controller result = _render_template_original( template, layout_name, locals) @view.controller.active_template = nil if @view.controller return result end end 
+3
source share

Use <% __FILE__ %> to get the full path to the file of the current view, but you can use it only inside the file itself, do not write some helpers

+3
source share

I had a similar question. I found <%= params[:controller] %> and <%= params[:action] %> according to my need, which was to add the controller name and action name as classes in the body tag.

Just in case, this helps anyone. :)

+1
source share

I am currently using a modified version of Peter Ehrlich's solution. The resulting string has the form controller_name/view_name , for example. users/new , which means that it can be passed directly to render later or modified for other purposes. I only tried this with Rails 4.2, although as far as I know it should completely go back to 3.xes.

 ActionView::Base.class_eval do attr_accessor :current_template end ActionView::TemplateRenderer.class_eval do def render_template_with_current_template_accessor(template, layout_name = nil, locals = {}) @view.current_template = template.try(:virtual_path) render_template_without_current_template_accessor(template, layout_name, locals) end alias_method_chain :render_template, :current_template_accessor end 
+1
source share

For the purpose of debugging, you can use the gem 'current_template' from here .

This gem checks the log file and displays the file name for the view / partial template.

For example:

enter image description here

Alternatively, you can simply add this line.

<%= "#{`tail log/development.log`}".scan(/\s[az]+\/\S+/) %>

to your layout / application.html.erb.

0
source share

All Articles