Ruby on rails 如何在模型中使用ApplicationViewHelper方法
我在文件Ruby on rails 如何在模型中使用ApplicationViewHelper方法,ruby-on-rails,model,view-helpers,Ruby On Rails,Model,View Helpers,我在文件app/helpers/application\u view\u helper.rb中有一个方法: module ApplicationViewHelper ... def cache_key_with_permission_for_all model ... end end 我尝试从模型访问它: class Classification < ActiveRecord::Base ... def self.get_current_cache_key "in
app/helpers/application\u view\u helper.rb中有一个方法:
module ApplicationViewHelper
...
def cache_key_with_permission_for_all model
...
end
end
我尝试从模型访问它:
class Classification < ActiveRecord::Base
...
def self.get_current_cache_key
"index-#{ActionController::Base.helpers.cache_key_with_permission_for_all Classification}"
end
end
分类
但我得到了一个错误:
NoMethodError - undefined method `cache_key_with_permission_for_all' for #<ActionView::Base:0x00000004e3ac70>:
app/models/classification.rb:140:in `get_current_cache_key'
NoMethodError-未定义的方法“cache_key_with_permission_for_all”for#:
app/models/classification.rb:140:in'get\u current\u cache\u key'
我错在哪里?您不应该在模型中使用助手方法。在视图中使用助手方法,而不是在模型中。当然,我也在视图中使用助手方法。这个助手正在为片段缓存生成一个键,我想使用同一个助手来生成我想在其他模型中的某些更改时使其失效的缓存键。此帮助程序必须位于视图帮助程序中,因为它调用允许的\u?从声明性授权中,仅在视图中可用。