向 Rails 3.1 资产管道添加自定义方法? [英] Add custom methods to Rails 3.1 asset pipeline?
问题描述
如何将我的自定义方法添加到我的资产中,例如 Rails 使用asset_path"助手所做的 css 文件?
How can I add my custom methods to my assets such as css files like Rails did with 'asset_path' helper?
有了 Rail 自己的助手,我可以这样写:
With Rail's own helper, I can write this:
# some.css.erb:
<%= asset_path 'a_image.png' %>
# How can I write this:
<%= my_custom_method 'a_image.png' %>
我尝试了很多方法,但找不到合适的方法.你知道吗?
I've tried many ways but couldn't found a decent way to this. Do you know one?
谢谢
推荐答案
我发现的最好方法是在 app/helpers 中创建自定义帮助器模块:
The best way I found was to create a custom helper module in app/helpers:
module AssetsHelper
def my_custom_helper_method
# do something
end
end
然后在 application.rb 中像这样要求它,在您的应用程序配置之后(最底部):
And then to require it like this in application.rb, after your applications configuration (very bottom):
module Sprockets::Helpers::RailsHelper
require Rails.root.join('app', 'helpers', 'assets_helper.rb')
include AssetsHelper
end
您可能会关注此问题以找到更好的方法:https://github.com/rails/rails/issues/3282
And you might follow this issue to find a better way: https://github.com/rails/rails/issues/3282
这篇关于向 Rails 3.1 资产管道添加自定义方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!