before_action是否属于私有方法的范围?

时间:2015-07-18 18:19:59

标签: ruby-on-rails ruby-on-rails-4

在控制器中:

before_action { |controller| controller.param_policy(action_name) }

同样的控制器中还有param_policy方法。在private下。为什么rails抱怨:

private method `param_policy' called for #<SomeController:0x007fc80196bc40>

代码:

class SomeController < ApplicationController
  before_action { |controller| controller.param_policy(action_name) }

  private

  def param_policy(action_name)
    # ...
  end
end

before_action方法范围之外的private是什么?

1 个答案:

答案 0 :(得分:3)

rails 4中用于将回调追加到控制器的正确语法是:

$_SESSION['parent']

在rails 3中,它是before_action :param_policy 。看到错误,您似乎在类before_filter的实例上调用param_policy,它不能调用私有方法,因为它是一个基本的OOPS概念。