Rails 3, find current view while in the layout

前端 未结 3 1888
长发绾君心
长发绾君心 2020-12-16 06:17

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 wra

3条回答
  •  半阙折子戏
    2020-12-16 07:07

    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'
    

提交回复
热议问题