Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get digested asset_path in model or controller

I have an Article model, which has a field svg_path.

I use fabricjs to draw a canvas that can contain multiple articles and is modifiable.

What I do currently is generating a json that contains all needed fields of the articles, including the svg_path.

When I try to use the asset_path helper (http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-asset_path) in either my Article model or controller it will always just return the path without the digest, which works in development environment but not in production. There I include the helper like this:

include ActionView::Helpers::AssetUrlHelper

asset_path(svg_path)

If I call this helper in the view it will generate the correct path including the digest hash.

How can I get the correct path in my json objects?

like image 569
IngoAlbers Avatar asked Nov 02 '15 09:11

IngoAlbers


People also ask

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

What does rake assets precompile do?

By default, Rails uses CoffeeScript for JavaScript and SCSS for CSS. DHH has a great introduction during his keynote for RailsConf. The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots.

How to enable asset pipeline in Rails?

The asset pipeline is implemented by the sprockets-rails gem, and is enabled by default. You can disable it while creating a new application by passing the --skip-asset-pipeline option.

What is Asset_path?

asset_path(source, options = {}) public. This is the entry point for all assets. When using the asset pipeline (i.e. sprockets and sprockets-rails), the behavior is “enhanced”. You can bypass the asset pipeline by passing in skip_pipeline: true to the options. All other asset *_path helpers delegate through this method ...


1 Answers

The solution was to call the helper like this:

ActionController::Base.helpers.asset_path(svg_path)

Before that I had included the helper as in my question, which didn't result in the expected result.

like image 141
IngoAlbers Avatar answered Oct 10 '22 15:10

IngoAlbers