Rails 3,在布局中查找当前视图

发布于 2024-10-17 01:49:35 字数 82 浏览 5 评论 0原文

我有一个场景,我想知道在布局文件中将要渲染的视图的名称。我可以找到解决方案来找到哪个布局将从视图中包装当前视图,但反之则不然。如何找到正在渲染的视图?

I have a scenario where I would like to have the name of the view that will be rendering while I'm in the layout file. I can find solutions to find which layout will be wrapping the current view from the view, but not the other way around. How can I find which view is rendering?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲世九天 2024-10-24 01:49:35

在Rails 3.0.3 中,我能够使用controller_name 和action_name 查看控制器和操作的名称。但这些都没有公开记录(至少是操作名称),所以我不会长期依赖它。

猴子补丁模板渲染可能会更好。在初始化程序中:

module ActionView::Rendering 
  alias_method :_render_template_original, :_render_template
  def _render_template(template, layout = nil, options = {}) 
    @last_template = template
    _render_template_original(template, layout, options)
  end
end

然后在布局中使用@last_template。

In Rails 3.0.3, I was able to see the name of the controller and action using controller_name and action_name. But those are not publicly documented (at least the action name) so I wouldn't depend on it long-term.

It might be better to monkey patch template render. In an initializer:

module ActionView::Rendering 
  alias_method :_render_template_original, :_render_template
  def _render_template(template, layout = nil, options = {}) 
    @last_template = template
    _render_template_original(template, layout, options)
  end
end

Then use @last_template in your layout.

孤芳又自赏 2024-10-24 01:49:35

以下解决方案适用于 Rails 3.1。将此代码放入初始值设定项中。
(rails 3.0.3 的答案在 Rails 3.1 中不再起作用)

这为每个控制器启用了 @active_template 变量。这是 ActionViewTemplate 类的实例。

active_template_virtual_path 方法返回模板作为名称,格式为“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

The following solution works in Rails 3.1. Place this code in an initializer.
(The rails 3.0.3 answer is not working any more in Rails 3.1)

This enables an @active_template variable for every controller. This is an instance of an ActionViewTemplate class.

The method active_template_virtual_path 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
椵侞 2024-10-24 01:49:35

我喜欢以下方法,因为在很多情况下您可以减少代码大小。将其包含在您的 application_controller.rb 中:

  before_filter :instantiate_controller_and_action_names
  caches_action :instantiate_controller_and_action_names

  def instantiate_controller_and_action_names
    @current_action = action_name
    @current_controller = controller_name
  end

然后,如果您的视图对应于某个操作,您只需执行以下操作:

dosomething if @current_action == 'new'

I like the following approach, as you can reduce code size a good bit in a lot of cases. Include this in your application_controller.rb:

  before_filter :instantiate_controller_and_action_names
  caches_action :instantiate_controller_and_action_names

  def instantiate_controller_and_action_names
    @current_action = action_name
    @current_controller = controller_name
  end

Then, if your views correspond to an action, you just do:

dosomething if @current_action == 'new'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文