Avoid *.js.erb files by building all the asset_path values

后端 未结 3 1479
無奈伤痛
無奈伤痛 2021-02-04 04:51

So I want to avoid processing JavaScript files with ERB just so I can get a proper asset path to, say, an image.

Currently, this seems like the popular approach:

3条回答
  •  有刺的猬
    2021-02-04 05:19

    An easier way :

    1. Get the assets prefix in your .js.erb : <%= Rails.configuration.assets.prefix %>. If an absolute path is needed, you can also get the application URL (it's more complicated to get it from rails, so you can just hardcode it in your .js.erb ?)

    2. If you are working with precompiled assets, get the fingerprint of your file which is stored in manifest.yml (at <%= Rails.configuration.assets.manifest %>). The manifest contains a list with all your assets and their respective fingerprints (documentation)

    3. Make assetPath just prepending the application URL + prefix to your image name or fingerprint

    An inconvenient is that you have to specify the full image name (included the extension).

提交回复
热议问题