Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use asset_url helper in Rake task

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)
like image 716
Jason Avatar asked Jun 29 '14 00:06

Jason


1 Answers

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

like image 168
Trung H. Tran Avatar answered Sep 28 '22 07:09

Trung H. Tran