Calling rails_asset_id directly in controller
Is there a way to call the private function rails_asset_id (http://apidock.com/rails/v3.0.5/ActionView/Helpers/AssetTagHelper/rails_asset_id) to generate a cache-busting key?
I need the same functionality for files that I'll be pulling down from an ajax request (so I can't just use the built in *_include_tag functions) that I want to be cached until they are modified. Is there a way to call rails_asset_id directly so that I don't have to re-imp开发者_运维百科lement the modified time checks and caching it does?
You can use send
to call private methods. For the file timestamp to work properly, rails_asset_id
requires the asset file to be inside assets_dir
, which is by default the /public
directory. Here's an example:
# Get asset id for the file /public/json/example.json
send(:rails_asset_id, 'json/example.json')
If you're using it outside view, this is one way to do it.
require 'action_view/helpers/asset_tag_helper'
include ActionView::Helpers::AssetTagHelper
send(:rails_asset_id, 'json/example.json')
Note that all of these techniques are hackish and will not work on future versions of Rails since they have changed the way the asset tag helpers are organized. So proceed with caution.
精彩评论