خطأ باستخدام طرق خاصة أو محمية في الفئات الفرعية من ApplicationController
-
22-09-2019 - |
سؤال
لدي مرشح مشترك بين بعض وحدات التحكم ، ويعلن في المقام الأول باعتباره خاصًا في 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
أحصل على هذا الخطأ:
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'
لماذا يحدث؟
المحلول
لا يمكنك إرسال رسائل خاصة/محمية مع جهاز استقبال صريح (كائن. يمكنك المحاولة c.send(:set_find_opts, User)
أو c.instance_eval { set_find_opts(User) }
.
لا تنتمي إلى StackOverflow