How do you get access to the asset_url
or asset_path
Sprockets URL helpers inside a Rake task?
I have a Rake task that seeds the database with some models. One of the models has the URL to an asset in the Rails app. How do I create a URL for this asset while inside the Rake task?
SomeModel.create(image: asset_url('awesome.png'))
For now I have a really poor solution to the issue.
path = URI.join(Rails.application.routes.url_helpers.root_url, '/assets/images/awesome.png')
SomeModel.create(image: path.to_s)
I've just tested out on rails 5.0.2
Add this into your rake class or controller class
include ActionView::Helpers::AssetUrlHelper
This will let you use all these methods http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With