Reputation: 3806
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)
Upvotes: 7
Views: 4408
Reputation: 59
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
Upvotes: 1
Reputation: 547
In your rake task:
include ActionView::Helpers::AssetUrlHelper
Upvotes: 0
Reputation: 8044
You have to include ActionView::Helpers
module Then the asset_path and its other helpers will be available.
include ActionView::Helpers
See Using helpers in model: how do I include helper dependencies?
Upvotes: 0