Ruby on rails 如何在视图助手中调用模型方法?
如何从视图辅助对象中的模型调用类方法。基本上,该视图根据传递给操作的类型ID列表显示搜索结果。但是在视图上,而不是在搜索ID上,我想显示这些产品的描述,而不是ID 用户从视图->控制器操作上的选择框中选择产品id,选择具有产品id的订单->结果视图具有订单详细信息,但屏幕顶部必须从具有id和说明的查找表中显示产品说明。 现在,当我尝试在助手方法中访问产品描述时,我得到一个 未初始化的常量DownloadHelper::MetaProductType异常 我知道我们可以在模型中调用helper方法,但是如何在helper中调用模型方法呢 产品型号如下:Ruby on rails 如何在视图助手中调用模型方法?,ruby-on-rails,ruby,model,helper,Ruby On Rails,Ruby,Model,Helper,如何从视图辅助对象中的模型调用类方法。基本上,该视图根据传递给操作的类型ID列表显示搜索结果。但是在视图上,而不是在搜索ID上,我想显示这些产品的描述,而不是ID 用户从视图->控制器操作上的选择框中选择产品id,选择具有产品id的订单->结果视图具有订单详细信息,但屏幕顶部必须从具有id和说明的查找表中显示产品说明。 现在,当我尝试在助手方法中访问产品描述时,我得到一个 未初始化的常量DownloadHelper::MetaProductType异常 我知道我们可以在模型中调用helper方法
class MetaProductType < ActiveRecord::Base
def self.get_product_type_description type_list
where('lower(product_type) in (?)', type_list).select('product_description')
end
end
辅助方法是:
module DownloadHelper
def print_hash(hash)
return_str = ''
return return_str if hash.nil?
hash.each_pair do |key, value|
key_arr = key.split('_')
return_str = return_str + key_arr.join(' ').capitalize
return_str = return_str + ' : '
if key.downcase.include? 'product'
#print product type descriptions
arr = value.split(',')
value = MetaProductType.get_product_type_description value
end
return_str = return_str + '<br />'
return_str.gsub!(/\w+/){$&.capitalize}
end
return (return_str)
end
我打算写一篇评论,但在这里会更容易: 语法 可能是错的,但我认为问题在于你的课堂教学方法 为什么不试试这个:
#app/models/meta_product_type.rb
class MetaProductType < ActiveRecord::Base
def self.get_product_type_description(type_list)
where('lower(product_type) in (?)', type_list).select('product_description')
end
end
#app/helpers/download_helper.rb
value = MetaProductType.get_product_type_description(value)
另一个问题可能是您在助手中自引用值var?为什么不重命名它,这样您就不用相同的变量了?Hi请为helper和model添加代码Hi请编辑问题,将代码放入问题中,并按照堆栈溢出约定设置格式4个空格缩进您确定值=MetaProductType.get\u product\u type\u描述值正在执行吗?它是否引发错误?此帮助程序是否位于帮助程序目录中?请在尝试执行此操作时添加web服务器控制台输出。