简体   繁体   English

如何在Rails 3模型中访问image_path路由助手?

[英]How can I access image_path route helper inside a Rails 3 model?

I'm trying to use Sprockets' image_path method from within a Rails 3 model. 我正在尝试从Rails 3模型中使用Sprockets的image_path方法。 I've tried including ActionView::Helpers::AssetTagHelper in my model and calling image_path but that does not work ( undefined local variable or method 'config' ). 我已经尝试在我的模型中包含ActionView::Helpers::AssetTagHelper并调用image_path但这不起作用( undefined local variable or method 'config' )。

Any ideas? 有任何想法吗?

Update - this worked for me: 更新 - 这对我有用:

ActionController::Base.new.view_context.asset_path("image.png")

I think this is the standard way of loading the asset helper: 我认为这是加载资产助手的标准方法:

ActionController::Base.helpers.asset_path("image.png")

This seems preferable to creating a new instance of ActionController::Base and asking for the view context in that it is more explicit. 这似乎比创建ActionController :: Base的新实例更好,并且要求视图上下文更明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM