Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `asset_data_uri' - Rail 3.2

I have a partial "person.css.erb":

 #caption {
   background-image: url(<%= asset_data_uri("caption.png") %>); 
   text-align: center;
 }

When the partial is rendered it fails with:

undefined method `asset_data_uri'

The rails asset pipeline guide has an example of using this method: http://guides.rubyonrails.org/asset_pipeline.html

Similar helpers work, eg. asset_path. I'm using Rails 3.2.8. Is the guide outdated? Was the method renamed? Do I need to do something special to get this helper included?

like image 279
Patrick Cullen Avatar asked Dec 15 '22 18:12

Patrick Cullen


1 Answers

I was running into the same error when using asset_data_uri in my view (asset_path worked) and couldn't figure out why. This isn't exactly your issue, but I was able to fix mine by adding this to my application_helper.rb:

# Copied from Sprockets::Context.asset_data_uri, and slightly modified.
def asset_data_uri path
  asset = Rails.application.assets.find_asset path

  throw "Could not find asset '#{path}'" if asset.nil?

  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
like image 117
Tim Coulter Avatar answered Dec 30 '22 16:12

Tim Coulter