Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在ApplicationController的子类中使用私有或受保护的方法时出错_Ruby On Rails_Before Filter - Fatal编程技术网

Ruby on rails 在ApplicationController的子类中使用私有或受保护的方法时出错

Ruby on rails 在ApplicationController的子类中使用私有或受保护的方法时出错,ruby-on-rails,before-filter,Ruby On Rails,Before Filter,我在一些控制器之间共享了一个过滤器,它主要在ApplicationController中声明为私有。此方法为控制器设置查找和分页条件 class ApplicationController < ActionController::Base ... protected # or private # Define parametros de busca def set_find_opts(klass) @filter = params[:f].to_i ||

我在一些控制器之间共享了一个过滤器,它主要在ApplicationController中声明为私有。此方法为控制器设置查找和分页条件

class ApplicationController < ActionController::Base
  ...
  protected # or private
    # Define parametros de busca
    def set_find_opts(klass)
      @filter = params[:f].to_i || nil
      
      @order = klass.set_order params[:o]
      
      @opts = { :page => params[:page] }
      @opts[:order] = @order if @order
    end
    ...
end

class Admin::UsersController < AdminController
  ...
  before_filter(:only => :index) {|c| c.set_find_opts User }
  ...
end
class ApplicationControllerparams[:page]}
@选择[:order]=@order如果@order
终止
...
终止
类Admin::UsersController:index){| c | c.set_find_opts User}之前
...
终止
我得到了这个错误:

  1) Error:
test_should_get_index(Admin::UsersControllerTest):
NoMethodError: protected method `set_find_opts' called for #<Admin::UsersControl
ler:0x848f3ac>
    app/controllers/admin/users_controller.rb:4
    functional/admin/users_controller_test.rb:9:in `test_should_get_index'
1)错误:
测试\u应该\u获取\u索引(Admin::UsersControllerTest):
NoMethodError:调用了受保护的方法“set\u find\u opts”#
app/controllers/admin/users\u controller.rb:4
功能/admin/users\u controller\u test.rb:9:in'test\u should\u get\u index'

为什么会发生这种情况?

您不能像在区块中那样使用显式接收器(object.protected\u方法)发送私人/受保护的消息。您可以尝试
c.send(:set\u find\u opts,User)
c.instance\u eval{set\u find\u opts(User)}

我怀疑它,但我不喜欢发布或使用send(:一些东西)。非常感谢你。