代码之家  ›  专栏  ›  技术社区  ›  Nate Smith

为什么所有Rails帮助程序始终可用于所有视图?有没有办法禁用此功能?

  •  88
  • Nate Smith  · 技术社区  · 17 年前

    为什么我可以在不同控制器的视图中访问一个控制器的帮助器方法?有没有一种方法可以在不攻击/修补Rails的情况下禁用此功能?

    4 回复  |  直到 17 年前
        1
  •  106
  •   Craig Walker    13 年前

    @George Schreiber的方法在Rails 3.1中不起作用;代码发生了重大变化。

    config.action_controller.include_all_helpers = false
    

    这将 prevent ApplicationController from loading all of the helpers

    (对于任何感兴趣的人, here's the pull request where the feature was created .)

        2
  •  98
  •   Simone Carletti    13 年前

    答案取决于Rails版本。

    钢轨>=3.1

    include_all_helpers 配置到 false 在要应用配置的任何环境中。如果要将配置应用于所有环境,请在中进行更改 application.rb .

    config.action_controller.include_all_helpers = false
    

    如果错误,它将 skip the inclusion

    Rails<3.1

    从中删除以下行 ApplicationController

    helper :all
    

        3
  •  27
  •   Marc-André Lafortune    15 年前

    在Rails 3中, actioncontroller/base.rb

    def self.inherited(klass)
      super
      klass.helper :all if klass.superclass == ActionController::Base
    end
    

    是的,如果你从 ActionController::Base ,将包括所有助手。

    要解决这个问题,请打电话 clear_helpers ( AbstractClass::Helpers ; 包括在 ActionController::Base

    # Clears up all existing helpers in this class, only keeping the helper
    # with the same name as this class.
    

    例如。:

    class ApplicationController < ActionController::Base
      clear_helpers
      ...
    end
    
        4
  •  5
  •   databyte    16 年前

    轨道2 ,ActionController::Base的默认功能是包含所有助手。

    Changeset 6222 on 02/24/07 20:33:47 (3 years ago) by dhh : 让它成为一个默认的假设,你需要所有的助手,所有的时间(是的,是的)

    更改:

    class ApplicationController < ActionController::Base 
      helper :all # include all helpers, all the time 
    end 
    

    从Rails 3 beta 1开始,这不再是变更日志中提到的情况: