向Rails 3.1资产管道添加自定义方法吗? [英] Add custom methods to Rails 3.1 asset pipeline?

查看:32
本文介绍了向Rails 3.1资产管道添加自定义方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将自定义方法添加到资产中,例如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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆