给你!您可以修改 Sprockets 以包含您的 url 选项助手...
module Sprockets
module Helpers
module RailsHelper
def asset_path_with_url_options(source, options = {})
uri = Addressable::URI.parse(asset_path_without_url_options(source, options))
default_url_options = url_options.dup.delete_if { |k,v|
[:host, :port, :protocol, :_path_segments, :script_name].include?(k)
}
uri.query_values = default_url_options.merge(uri.query_values || {})
uri.to_s
end
alias_method_chain :asset_path, :url_options
alias_method :path_to_asset, :asset_path_with_url_options # aliased to avoid conflicts with an asset_path named route
end
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
# anything using link_to, url_for, etc. uses this automatically.
def url_options
{
:k1 => v1,
:k2 => v2
}.merge(super)
end
end